将 GET 请求写入 return HTTP 1.1 而不是 1.0
Writing a GET request to return HTTP 1.1 instead of 1.0
我正在 python 中编写一个 tcp 网络客户端和网络服务器(我是 python 和 sockets/networking 的新手)。一切正常,但我想实施 HTTP/1.1 协议而不是 HTTP/1.0,无论我做什么我都无法得到 1.1 的响应。我读到它需要一个主机字段,但也许我做错了什么。
这是我的 GET 请求
request = "GET " + path + " HTTP/1.1" + "\r\nHost: " + HOST + "\r\n\r\n"
这是我的回复
"b'HTTP/1.0 200 OK
我正在为我的网络服务器使用 BaseHTTPRequestHandler。这可能是问题所在吗?
您应该在 BaseHTTPRequestHandler
实施中将 protocol_version
设置为 'HTTP/1.1'
:
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 accurate Content-Length
header (using send_header()
) in all of its responses to clients. For backwards compatibility, the setting defaults to 'HTTP/1.0'
.
试试:request = f"GET / HTTP/1.1\r\nHost: {HOST}:{PORT}\r\n\r\n".encode()
在 {HOST} 中插入主机,在 {PORT} 中插入端口。
我正在 python 中编写一个 tcp 网络客户端和网络服务器(我是 python 和 sockets/networking 的新手)。一切正常,但我想实施 HTTP/1.1 协议而不是 HTTP/1.0,无论我做什么我都无法得到 1.1 的响应。我读到它需要一个主机字段,但也许我做错了什么。
这是我的 GET 请求
request = "GET " + path + " HTTP/1.1" + "\r\nHost: " + HOST + "\r\n\r\n"
这是我的回复
"b'HTTP/1.0 200 OK
我正在为我的网络服务器使用 BaseHTTPRequestHandler。这可能是问题所在吗?
您应该在 BaseHTTPRequestHandler
实施中将 protocol_version
设置为 'HTTP/1.1'
:
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'
.
试试:request = f"GET / HTTP/1.1\r\nHost: {HOST}:{PORT}\r\n\r\n".encode()
在 {HOST} 中插入主机,在 {PORT} 中插入端口。