Checkpoint 13.4.1.
Q-2: The ________ header indicates that the body of the document is a jpeg image.
import socket
import time
HOST = 'data.pr4e.org'
PORT = 80
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((HOST, PORT))
mysock.sendall(b'GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n')
count = 0
picture = b""
while True:
data = mysock.recv(5120)
if len(data) < 1: break
#time.sleep(0.25)
count = count + len(data)
print(len(data), count)
picture = picture + data
mysock.close()
# Look for the end of the header (2 CRLF)
pos = picture.find(b"\r\n\r\n")
print('Header length', pos)
print(picture[:pos].decode())
# Skip past the header and save the picture data
picture = picture[pos+4:]
fhand = open("stuff.jpg", "wb")
fhand.write(picture)
fhand.close()
Content-Type
header indicates that body of the document is an image (image/jpeg
). Once the program completes, you can view the image data by opening the file stuff.jpg
in an image viewer.recv()
method. We get as many characters as have been transferred across the network to us by the web server at the moment we call recv()
. In this example, we either get as few as 3200 characters each time we request up to 5120 characters of data.recv()
we get 3167 bytes, which is the end of the stream, and in the next call to recv()
we get a zero-length string that tells us that the server has called close()
on its end of the socket and there is no more data forthcoming.recv()
calls by uncommenting the call to time.sleep()
. This way, we wait a quarter of a second after each call so that the server can “get ahead” of us and send more data to us before we call recv()
again.recv()
, we now get 5120 characters each time we ask for new data.send()
requests and our application making recv()
requests. When we run the program with the delay in place, at some point the server might fill up the buffer in the socket and be forced to pause until our program starts to empty the buffer. The pausing of either the sending application or the receiving application is called “flow control.”