--> Skip to main content

Featured

Steps to Create a Project on GitHub

Steps to create a project on GitHub:  1.   Start a repo on GitHub 2.   Make a README.md file 3.   Open vs code – new folder – new terminal – git clone http:…. (from the repo). 4.   In terminal – cd theprojectname   à move into the project file 5.   Ls -la is for showing hidden file à not working for me ???? 6.   Make some changes to README file, in terminal git status à shows all the changes on the file 7.   To add all changes update to the folder à git add . (the dot . means all changes to be added) 8.   Add a new file index.html, in terminal à git commit -m “title of commit” -m “description of commit” 9.   Then git push origin master 10.                 ****Initial a repo in local text editor à git init 11.                 After use git add . etc, when pus...

HTTP 1.0 vs. HTTP 1.1 - 400 Bad Request May Occur When a Client Send Request Using Socket Module

Two things to address in this blog.
 
1. HTTP 1.1 requires that you transmit a Host header with all requests, which is written here in RFC 1626 section 14.23. For Http 1.0 though this is not required. I've commented in this little program for both situations.

2. In the while loop,
'if not data:break'
works, which means it can successfully stop the while loop when data has been fully transmitted. If using
'if len(data)<0: break="" pre=""> < 0'
however, the program will infinitely pausing till new data coming in.
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                 

s.connect(("info.cern.ch" , 80))

# 1. use HTTP/1.0 when send out with HTTP/1.0, response is 200 OK, 
s.sendall("GET http://info.cern.ch HTTP/1.0\r\n\r\n".encode())

# 2. use HTTP/1.1 when send out with HTTP/1.1, response is 400 Bad Request without HOST header
# s.sendall("GET / HTTP/1.1\r\nHost: info.cern.ch\r\n\r\n".encode())

while True:
    data = s.recv(4096)
    if not data: break
    print (data.decode(), end=" ")
s.close()

The following should be printed in the terminal:

Popular Posts