1

I am kinda new to Python, but I really want to know, what does that mean.

In my book, I have a code:

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()

And the response is:

$ python urljpeg.py
5120 5120
5120 10240
4240 14480
5120 19600
...
5120 214000
3200 217200
5120 222320
5120 227440
3167 230607
Header length 393
HTTP/1.1 200 OK
Date: Wed, 11 Apr 2018 18:54:09 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Mon, 15 May 2017 12:27:40 GMT
ETag: "38342-54f8f2e5b6277"
Accept-Ranges: bytes
Content-Length: 230210
Vary: Accept-Encoding
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Connection: close
Content-Type: image/jpeg

I would like to know: - What do these number in the beginning of the response mean? - Could anyone recommend a source to read about retrieving image for dummies?

1
  • The numbers at the beginning of the response are the result of "print(len(data), count)" Commented Nov 8, 2018 at 17:12

2 Answers 2

1

They are the result of

print(len(data), count)

Respectively, the length of the data and the total of data it has received so far.

Sign up to request clarification or add additional context in comments.

3 Comments

But I mean what it receives from? The data from the image? How is it proceed?
It receives the image data by splitting them into chunks of size 5120. For each chunk of data it receives, it prints the length of the data and the length of data it has received so far. Finally, they are joined to get the original image.
Thank you a lot. Am i right saying that it is some data in numbers about the image, and after getting the whole information we can finally retrieve the image?
1

Since data transfers through a computer network divides the data in packets, you don't receive the whole data at once. Therefore what that code is doing is receiving "chunks" of data and what's printed is the size of the "chunk" and the total amount of data received (by the line print(len(data), count)).

Once all the data has been received, you can extract the picture data. Since you are working with HTTP, the data you receive will be an HTTP response. An HTTP response has two parts a header, which is a series of key-value pairs that give information about the response itself and is what's being printed at the end of your output; and the content which is the data and in your case contains the picture.

In HTTP header and content are separated by two line endings \r\n\r\n. Therefore picture.find(b"\r\n\r\n") looks the position where the header ends and the content (the image) begins.

If all you need to do is retrieve images using HTTP, I would suggest taking a look into the requests package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.