I am trying to send and receive HTTP data using the Python socket. But when I use HTTP/1.0 it works but when I use HTTP/1.1 it just keeps waiting...
This code works
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()
This doesn't work
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()
If the HTTP/1.1 is the problem then how could I detect if it doesn't support HTTP/1.1?
urllib2? HTTP is not a simple protocol, there are lots of details you need to understand.