Python Socket Programming Summary

Source: Internet
Author: User

1. Client Connection

There are 4 steps to connecting a client:

(1) Create a Socket object

Socket=socket.socket (Family,type)

(2) Connect to the server using the socket's Connect method

Socket.connect ((Host,port))

Where host is the IP address of the server and if only the domain name of the server is known, it can be obtained through host = Socket.gethostbyname (hostname).

Port is the port number opened by the server, and the service information is found through Port=socket.getservbyname (Servicename[,protocolname]), which translates the name of the Internet service name and protocol into the port number. ProtocolName is the protocol name, if any, should be "TCP" or "UDP", otherwise, any protocol will match.

(3) The client and server communicate through the Send and recv methods.

(4) The client closes the connection by invoking the Close method of the socket.

2. Server Connection

There are 6 steps to establishing a server connection:

(1) Set up the socket object;

(2) Bind the socket to the specified address, s.bind (("IP", Port))

(3) Listening connection, socket.listen (backlog), the backlog specifies the maximum number of connections, at least 1, after receiving a connection request, these requests must be queued and if the queue is full, the request is rejected.

(4) The server socket waits for a client to request a connection through the socket's Accept method:

Connection,address=socket.accept ()

When the Accept method is called, the socket enters the ' waiting ' (or blocked) state. When a client requests a connection, the method establishes the connection and returns the server. The Accept method returns a tuple that contains two elements, such as (connection,address). The first element (connection) is the new socket object through which the server communicates with the customer, and the second element (address) is the customer's Internet address.

(5) Processing stage, the server and the customer communicate via send and recv method (transmit data). The server calls send and sends a message to the customer in string form. The Send method returns the number of characters that have been sent. The server uses the Recv method to accept information from the customer. When calling recv, you must specify an integer to control the maximum amount of data that is accepted by this call. The Recv method enters the ' Blocket ' state when the data is accepted, and finally returns a string that represents the received data. If the amount sent exceeds the allowable recv, the data is truncated. The excess data will be buffered on the receiving end. When you call recv later, the extra data is removed from the buffer.

(6) At the end of the transfer, the server calls the socket's Close method to close the connection.

Resources:

1, http://www.jb51.net/article/52078.htm

Python Socket Programming Summary

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.