我正在尝试使用Python套接字发送和接收jupyter
数据。但是当我使用HTTP
时,它可以工作,但是当我使用HTTP/1.0
时,它一直在等待...
此代码有效
HTTP/1.1
这不起作用
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('httpbin.org', 80)
client_socket.connect(server_address)
request_header = 'GET /ip HTTP/1.0\r\nHost: httpbin.org\r\n\r\n'
client_socket.send(request_header.encode())
response = ''
while True:
recv = client_socket.recv(1024)
if not recv:
break
response += recv
print(response)
client_socket.close()
如果是import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('httpbin.org', 80)
client_socket.connect(server_address)
request_header = 'GET /ip HTTP/1.1\r\nHost: httpbin.org\r\n\r\n'
client_socket.send(request_header.encode())
response = ''
while True:
recv = client_socket.recv(1024)
if not recv:
break
response += recv
print(response)
client_socket.close()
的问题,那么我如何检测它是否不支持HTTP/1.1
?
答案 0 :(得分:1)
HTTP / 1.1默认为持久连接。如果希望服务器在发送响应后关闭连接,则需要发送Connection: close
标头。
request_header = 'GET /ip HTTP/1.1\r\nHost: httpbin.org\r\nConnection: close\r\n\r\n'