How to use TCP-based HTTP to download image in python? I do download the image but it says cannot open this file( which probably means not all of the bytes were recv or written). My task is to use socket library and no urlib or requests. Any help is appreciated.
serverPort = 80
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(('google.com', serverPort))
print("ready to receive!")
output = 'GET http://google.com/favicon.ico HTTP/1.0\r\nHOST: google.com\r\n\r\n'
print(output)
output1 = ('b' + output)
clientSocket.sendall(output1.encode())
reply = b''
while True:
data = clientSocket.recv(1024)
if not data:
break
reply += data
headers = reply.split(b'\r\n\r\n')[0]
image = reply[len(headers) + 4:]
f = open('image_test.ico', 'wb')
f.write(image)
f.close()
clientSocket.close()
requests.