Soket client program

Source: Internet
Author: User

" "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

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.