I'm writing a very basic HTTP client:
import socket
from socket import *
Payload = """GET /test.html HTTP/1.1
Accept: */*
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
Accept-Encoding: gzip, deflate
Proxy-Connection: Keep-Alive
Host: example.com
Pragma: no-cache
"""
def SendAndReceive(Host, Payload):
s = socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.connect(Host)
s.sendall(Payload)
tdata=[]
while True:
data = s.recv(1024)
if not data:
break
tdata.append(data)
print ''.join(tdata)
return ''.join(tdata)
SendAndReceive(("www.example.com",80),Payload)
For some reasons, the recv() stalls for a while (~10 sec), then returns the data. I'm not sure what's wrong with my code, any help would be greatly appreciated.
Thanks!