Following is my code:
import socket
import time
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(b'GET /code/romeo.txt HTTP/1.1\n')
mysock.send(b'Host: www.py4inf.com\n\n')
all = b""
while True:
data = mysock.recv(512)
all = all + data
if len(data) < 1:
break
mysock.close()
stuff = all.decode()
position = stuff.find('\r\n\r\n')
print(stuff[position+4:])
There must be something wrong because it takes almost 30 seconds to invoke break in while loop.
However, if I change the code if len(data) < 1:
to if len(data) < 100:
it took just 0.5 second.
Please help. It haunted me for a while. The sample website: http://www.py4inf.com/code/romeo.txt