" "The Soket client mainly completes the following steps: 1. Establish a soket socket (to interpret a socket as a channel) 2. Establish a connection 3. Send an HTTP request to the server 4. Data received 5. Close connection 6. Data processed locally" "ImportSocket#Importing the socket library" "establish a socket,af_inet to represent the IPv4 protocol (AF_INET6 represents the IPv6 protocol) and sock_stream to use a stream-oriented TCP protocol (SOCK_DGRAM represents a datagram socket, or UDP)" "s=Socket.socket (Socket.af_inet,socket. Sock_stream) S.connect (('www.qq.com', 80))#Establish a connection#send HTTP requests to the server using the HTTP1.1 Protocol (format)S.send (b'get/http/1.1\r\nhost:www.qq.com\r\n\r\nconnection:closer\r\n\r\n')#define a list to receive text Format filesBuffer=[]#continuous Scan to see if there's any data sent. whiletrue:d= S.RECV (2048)#the size of each receive data ifD:buffer.append (d)#append each received data to buffer Else: Break #If no data is sent, jump out of the loop .Data= b"'. Join (Buffer)#b represents the byte form, creates a new data byte, and joins the bufferS.close ()#Link Closeheader,html= Data.split (b'\ r \ n', 1)Print(Header.decode ('Utf-8') ) with open ('G:\sina.html','WB') as F:f.write (HTML)
Soket client program