Search code examples
pythonimagedata-retrieval

Why there is double\r\n instead of \r\n in the codes


original codes from PY4E:

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/cover.jpg HTTP/1.0\n\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()

my questions are little more:

  • what is the mean of picture=b''
  • what is pos for?
  • What is [pos+4:] inpicture=picture[pos+4:] for?
  • How can I view the image? Thanks for any guidance in advance.

Solution

  • what is the mean of picture=b''

    This is setting the picture variable to an empty byte array, which will be added to later.

    what is pos for?

    In pos = picture.find(b"\r\n\r\n"), the code is looking for the end of the HTTP header that comes before the picture data.

    What is [pos+4:] in picture=picture[pos+4:] for?

    The comment above it explains it, it "Skips past the header" to just get the picture data.

    How can I view the image?

    In these two lines,

    fhand = open("stuff.jpg", "wb")
    fhand.write(picture)
    

    the picture is saved to a file titled stuff.jpg, you should be able to double click in a file manager to open it.