I'm writing a tcp web client and web server in python (and I'm new to python and sockets/networking). Everything is working but I'd like to implement the HTTP/1.1 protocol rather than HTTP/1.0 and I can't get a response of 1.1 no matter what I do. I read that it requires a host field, but maybe I'm doing something else wrong.
Here is my GET request
request = "GET " path " HTTP/1.1" "\r\nHost: " HOST "\r\n\r\n"
Here is my response
"b'HTTP/1.0 200 OK
I'm using BaseHTTPRequestHandler for my web server. Is that the problem maybe?
CodePudding user response:
You should set the protocol_version
to 'HTTP/1.1'
in your BaseHTTPRequestHandler
implementation:
protocol_version
This specifies the HTTP protocol version used in responses. If set to'HTTP/1.1'
, the server will permit HTTP persistent connections; however, your server must then include an accurateContent-Length
header (usingsend_header()
) in all of its responses to clients. For backwards compatibility, the setting defaults to'HTTP/1.0'
.
CodePudding user response:
Try: request = f"GET / HTTP/1.1\r\nHost: {HOST}:{PORT}\r\n\r\n".encode()
insert host in {HOST} and port in {PORT}.