Seven. Python Network programming socket programming

Source: Internet
Author: User

Steps for Python to write server:

1 The first step is to create the socket object. Call the socket constructor. Such as:

Socket = Socket.socket (family, type)

The family parameter represents the address family, which can be either af_inet or Af_unix. The Af_inet family includes Internet addresses, and the Af_unix family is used for interprocess communication on the same machine.

The type parameter represents a socket type, which can be sock_stream (stream sockets) and Sock_dgram (datagram sockets).

2. The second step is to bind the socket to the specified address. This is done through the bind method of the Socket object:

Socket.bind (address) the socket created by AF_INET, the address must be a two-element tuple in the format (host,port). Host, Port represents the port number. The Bind method throws an Socket.error exception if the port number is in use, the hostname is incorrect, or the port has been persisted.

3. The third step is to receive the connection request using the Listen method of the socket socket.

Socket.listen (Backlog)

The backlog specifies the maximum number of clients that are allowed to connect to the server. It has a value of at least 1. When a connection request is received, the requests are queued and the request is rejected if the queue is full.

4. The fourth step is that the server socket waits for the client to request a connection through the socket's accept method.

Connection, address =socket.accept ()

When the Accept method is called, the socket is in the "Waiting" state. When a client requests a connection, the method establishes the connection and returns the server. The Accept method returns a tuple containing two elements (connection,address). The first element, connection, is the new socket object that the server must communicate with the customer, and the second element address is the customer's Internet location.

5. The fifth step is the processing stage where the server and the client communicate via the Send and Recv methods (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 receive information from the customer. When calling recv, the server must specify an integer that corresponds to the maximum amount of data that can be received through this method call. The Recv method enters the "blocked" state when it receives the data, and finally returns a string that represents the received data. If the amount of data sent exceeds the allowable recv, the data will be truncated. The excess data will be buffered on the receiving side. When Recv is called later, the extra data is removed from the buffer (and any other data that the customer may have sent since the last call to Recv).

6. When the transfer is complete, the server calls the Close method of the socket to close the connection.

Steps for Python to write the client:

1. Create a socket to connect to the server: socket= socket.socket (family, type)

2. Connect to the server using the Connect method of the socket. For the Af_inet family, the connection format is as follows:

Socket.connect ((Host,port))

Host represents the server host name or ip,port the port number that is bound on behalf of the server process. If the connection succeeds, the client can communicate with the server through the socket, and if the connection fails, a Socket.error exception is thrown.

3. During the processing phase, the client and the server communicate via the Send method and the Recv method.

4. When the transfer is complete, the client closes the connection by calling the Close method of the socket.

server.pyImportSockets=socket.socket (socket.af_inet, socket. SOCK_STREAM)#create a socket based on the IPV4 and TCP protocolS.bind (("127.0.0.1", 123))#Listening PortS.listen (5) whiletrue:cnn,addr=s.accept ()#Accept new Connectionsmsg='Welcome to visit! 'cnn.send (Msg.encode ("Utf-8")) Cnn.close

client.py Import sockets=socket.socket (socket.af_inet,socket. Sock_stream) S.connect (("127.0.0.1", 123= S.RECV (1024x768)#  receive less than 1024 bytes of data s.close ()print(Msg.decode ("utf-8"))

Seven. Python Network programming socket 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.