Python for Infomatics Chapter 12th Network Programming II (translation)

Source: Internet
Author: User
Tags ranges

Note: The following article was originally from Dr. Charles Severance's Python for informatics

12.3 Getting a picture with the HTTP protocol

In the example in the previous section, we get a text file with a newline character and simply display it on the screen. Similarly, we can use a small program to get pictures through the HTTP protocol. When the following program runs, instead of displaying the data directly on the screen, it rejects the header information and then merges the received data into a picture file. The specific code is as follows:

ImportSocketImportTimemysock=Socket.socket (socket.af_inet, socket. Sock_stream) Mysock.connect (('www.py4inf.com', 80)) Mysock.send (b'GET http://www.py4inf.com/cover.jpg http/1.0\n\n') Count=0picture= b"" whileTrue:data= Mysock.recv (5120)    if(Len (data) < 1 ) :         Break    #Time.sleep (0.25)Count = Count +len (data)Print(len (data), count) picture= picture +datamysock.close ()#Look for end of the header (2 CRLF)pos = Picture.find (b"\r\n\r\n")Print('Header Length', POS)Print(picture[:p Os].decode ("Utf-8"))#Skip past the header and save the picturePicture = Picture[pos+4:]fhand= Open ("stuff.jpg","WB") fhand.write (picture) fhand.close ()

Run the program with the output as follows:

5120 5120
5120 10240
5120 15360
5120 20480
5120 25600
5120 30720
960 31680
5120 36800
5120 41920
2384 44304
3752 48056
5120 53176
5120 58296
5120 63416
5120 68536
1767 70303
Header Length 242
http/1.1 OK
Date:sat, APR 06:51:38 GMT
Server:apache
Last-modified:fri, Dec 19:05:04 GMT
ETag: "B294001f-111a9-526172f5b7cc9"
Accept-ranges:bytes
content-length:70057
Connection:close
Content-type:image/jpeg

As you can see, its content Type header information indicates that the body of the document is a picture. Once the program is finished, you can open the Stuff.jpg file with the picture Viewer, and you will find that this is the cover of the book.

From the process of running the program, you can see that we do not receive 5,120 characters at a time. At the moment we called the Recv () method, we received as many characters as the Web server sent us. In this example there is a 960,2384 to a maximum of 5,120 characters. Because your Internet connection will vary, your output will be different too. Also note that the last stream we receive is 1767, the next call to Recv () we will receive a 0-length string, which tells us that the server has called close () to close the socket on its end, and no more data will be sent.

We can cancel the Time.sleep () comment and slow down the time interval of our successive calls to Recv (). In this way, we wait for a quarter of a second before each call, so that the server catches up with us sending more data before calling Recv (). After adding the delay, the program runs the following results:

5120 5120

5120 10240
....
5120 66560
3743 70303
Header Length 242
http/1.1 OK
Date:sat, APR 07:38:55 GMT
Server:apache
Last-modified:fri, Dec 19:05:04 GMT
ETag: "B294001f-111a9-526172f5b7cc9"
Accept-ranges:bytes
content-length:70057
Connection:close
Content-type:image/jpeg

Now in addition to the last call of less than 5120, we have received 5,120 each time (note: May be the reason for the speed, the translator's first receive is also 5,120, and the original text is less than 5120). This is the buffer capacity that the server sends and we receive the common contract. When we take delay to receive, at some point the server's send may fill this buffer, and thus have to stop sending, only to our program to clear the buffer. This pause between the sending and receiving programs is called flow control.

Python for Infomatics Chapter 12th Network Programming II (translation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.