Python Socket Network Programming

Source: Internet
Author: User

1 #client and server:2 Most connections are reliable TCP connections. When you create a TCP connection,3 actively initiating a connected call client,4 the passive response connects to the called server. 5 6 ##关于端口7 port numbers less than 1024 are ports for Internet standard services, such as the SMTP service is 25 ports, the FTP service is 21 ports, and so on8 the port number is greater than 1024 and can be used arbitrarily. 9 to proactively initiate a TCP connection, the client must know the IP address and port number of the server. Ten as a server, the port number must be fixed to provide what kind of service.  One  A ##关于发送数据怎么协调 - A TCP connection creates a bidirectional channel in which both parties can send data to each other.  - However, who first sent after the hair, how to coordinate, according to the specific agreement to decide.  the For example, the HTTP protocol specifies that the client must send a request to the server before the server sends the data to the client.  -  ----------------------Client---------------------------------- -  + #To Import the socket library: - ImportSocket +  A #Create a socket: ats = socket.socket (socket.af_inet, socket. SOCK_STREAM)##AF_INET指定使用IPv4协议, if you want to use a more advanced IPv6, specify it as Af_inet6. SOCK_STREAM Specifies the use of a stream-oriented TCP protocol - #To establish a connection: -S.connect (('www.sina.com.cn', 80))##注意参数是一个tuple that contains the address and port number.  - #Send data: -S.send (b'get/http/1.1\r\nhost:www.sina.com.cn\r\nconnection:close\r\n\r\n')##建立TCP连接后, we can send a request to Sina server, request to return the content of the homepage: -  in  - #Receive data: toBuffer = [] +  whileTrue: -     #receive up to 1k bytes at a time: theD = S.RECV (1024) *     ifD: $ Buffer.append (d)Panax Notoginseng     Else: -          Break thedata = b"'. Join (Buffer)##接收数据时, the recv (max) method is called, which receives the specified number of bytes at a time, so it is received repeatedly in a while loop until recv () returns empty data, indicating that the receive is complete and exits the loop.  +  A  the #To close the connection: +S.close ()##当我们接收完数据后, call the Close () method to close the socket so that a complete network communication is over -  $  $ #the data received include the HTTP header and the Web page itself, we just need to separate the HTTP header and Web page, the HTTP header print out, the content of the Web page saved to the file -Header, html = data.split (b'\r\n\r\n', 1) - Print(Header.decode ('Utf-8')) the #to write the received data to a file: -With open ('sina.html','WB') as F:Wuyi f.write (HTML) the  - ##最后, now, just open the sina.html file in the browser, you can see the homepage of Sina.  Wu  --------------------------Service-side----------------------------------------

Python Socket Network Programming

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.