Create a server through the socketserver library in Python

The socketserver library is Python's standard library and provides a socket server framework through which the server creation process can be simplified.

1 Import of socketserver library

Import the socketserver library through the code shown in Figure 1.

Figure 1 Import socketserver library

2 Create a TCP server through the socketserver library

Creating a TCP server through the socketserver library is divided into the following steps: first define a derived class of the BaseRequestHandler class in the socketserver library; then rewrite the handle() method of the derived class; finally instantiate the TCPServer class in the socketserver library and start it through this instance Server.

2.1 Define the derived class of the BaseRequestHandler class in the socketserver library

To create a server, you must define an instance of the TCPServer class in the socketserver library. When defining the instance, you need to specify a request handler object, which defines how the server handles client requests. The BaseRequestHandler class is the parent class of all request handler object classes. Therefore, you need to first define a derived class of the BaseRequestHandler class in the socketserver library. The code is shown in Figure 2.

Figure 2 Defines a derived class of the BaseRequestHandler class in the socketserver library

Among them, MyTCPHandler is a custom class, which is a derived class of the BaseRequestHandler class in the socketserver library. In MyTCPHandler, the handle() method is rewritten, and the function of this method is to define how to handle the client's request. Line 5 defines the data attribute of MyTCPHandler. self.request is an attribute of the parent class and represents the socket that communicates with the client. recv() represents receiving data from the client and saving it to the data attribute; Line 6 self.client_address in is an attribute of the parent class, indicating the list of clients connected to the server; line 7 prints out the data sent by the client; line 8 uses self.request, which is the socket, and calls the sendall() method to receive the After the received data is changed to uppercase, it is sent to the client.

2.2 Start the server

Start the server through the TCPServer class in the socketserver library. The code is shown in Figure 3.

Figure 3 Start the server

Among them, the 11th line of code specifies the IP address of the server and the listening port number; the 12th line of code defines the instance of the socketserver.TCPServer class, server, through the with...as statement, and sets the server's IP address and listening port number. The number and the custom request processing class MyTCPHandler are passed to the instance; line 13 of the code starts the server by calling the serve_forever() method on the server.

Related Links 1 Please refer to the with...as statement

The with as statement in Python_Mianhou’s Blog-CSDN Blog

3 Operation effect

First run the server, and then run it in the windows10 system and windows7 system respectivelyPython network programming improved version client-CSDN blog

For the client mentioned in , the running effect of the server at this time is shown in Figure 4.

Figure 4 Server operation effect

At this time, the client running effect is shown in Figure 5.

Figure 5 Server operation effect

Guess you like

Origin blog.csdn.net/hou09tian/article/details/134292113