Download pictures using Socket

Socket Download picture

 

In the Baidu search head, pick a

 Copy the image path to open and save this url

To separate the url of the domain name and path

 

 First import socket module

 

 Step 1: Create a client socket

 

 Step two: initiate a connection, connect () parameters fill the domain name and port, http protocol port 80 by default

 

 The third step: configuration message

 

Message in the URL is a picture from the start address and the domain name points out the path, and then the message written on one line

 

 The fourth step: sending an HTTP request

 In this case will run error

Because data is transmitted and received Socket of bytes required Byte, present or string, by encode () encoding

 

 Fifth step: in response to receiving the content loop

 Because they do not know the content of the response of the specific number, each 1024 bytes taken from the pipeline, taking up completed, then print out the RES, to look at the data received

 

 Step Six: By regular matches image (minus response header)

 This is also the starting data bytes to match the type of match is a complete list of return

 

The final step: the picture is written to the local

 The contents of the picture itself is binary, do not decode () decoded

 

Guess you like

Origin www.cnblogs.com/jiyu-hlzy/p/11746292.html