Python | Http request header

Http request header

In the previous section, we mainly talked about the response header. We did not pay attention to what is in the request header. Run the following code:

Import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('192.168.31.199', 8090))
server_socket. listen(128)
while True:
client_socket, client_addr = server_socket. accept()
data = client_socket.recv(1024).decode('utf8')
print('Received {} data{}'.format(client_addr[0], data))
client_socket.send('HTTP/1.1 200 OK '.encode('utf8'))
client_socket.send('content-type: text/html '.encode('utf8'))
client_socket.send(' '.encode('utf8'))

client_socket.send('

hello world

'.encode('utf8'))
Browser access results:

The console output is as follows:

The first sentence of the request header GET / HTTP/1.1 means:

GET request method, GET/POST/PUT/DELETE... ...
/ path requested
HTTP/1.1 HTTP version number
At this point we change the access path:

You can see that the first sentence of the request header changes to GET /index.html?name=jack&age=18 HTTP/1.1, indicating that the middle part can also represent the parameters of the request.
The second sentence of the request header, Host: 192.168.31.199:8090, indicates the requested server address.
I don't care about other information for the time being, it is browser-related information. A brief explanation of UA: user agent, the purpose of the initial design is to identify the type of browser from the request header.

IP address binding

The ip address can only be accessed through the ip address. If you change the bound address to 127.0.0.1, you can use this address to access it in the browser (or visit localhost):

The 0.0.0.0 we used in the previous sections can also represent the local machine, which represents all available addresses. If 127.0.0.1 is used, it can only be accessed through the local machine and cannot be accessed by others. It can be seen that if it is on the cloud server For deployment, it is more convenient to use 0.0.0.0.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us