The simplest socket server for Python socket programming

Source: Internet
Author: User

First, the socket to write the server steps:


1. The first step is to create the socket object. Call the socket constructor. Such as:
    1. 1 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:
1 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 use the Listen method of the socket socket to receive the connection request.
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.
1 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 phase, the server through the send and Recv method communication (transfer 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, the transmission ends, the server calls the socket's Close method to close the connection.
Source: Second, the complete code is as follows:
1 #Coding=utf-82 ImportSocket3Buf_size = 10244Host ='localhost'5Port = 80826 7Server =Socket.socket (socket.af_inet, socket. SOCK_STREAM)8 Server.bind ((host, port))9Server.listen (10)#Concurrency numberTen  whileTrue: OneClient, address =server.accept () Adata =client.recv (buf_size) - client.send (data) -     Print(Data.decode ())#Python3 to use decode theClient.close ()

Third , client request:

Using AB test: AB-C 1 http://localhost:8082 results are as follows: get/http/1.0
host:localhost:8083
User-agent:apachebench/2.0.40-dev
Accept: */*




From for notes (Wiz)



The simplest socket server for Python 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.