python socketserver module overview

Translation from: https: //docs.python.org/3.7/library/socketserver.html

 

socketserver module simplifies the task of writing network servers.

There are four basic categories of specific services:

  • class socketserver.TCPServer(server_address, RequestHandlerClass, bind_and_activate=True)
  • class socketserver.UDPServer(server_address, RequestHandlerClass, bind_and_activate=True)
  • class socketserver.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True)
  • class socketserver.UnixDatagramServer(server_address, RequestHandlerClass, bind_and_activate=True)
Generating a service requires the following steps:

First, you must create a request handler class (class request processing), the method is to subclass BaseRequestHandler class and cover its handle () method, which will handle incoming requests.

Second, one must instantiate service class, and the server's address (address of the server), and the request handler class (class request processing) passed to it (service class). Recommended to use with the service in the statement. Then call the server object handle request () or serve forever () to process one or more requests.

Finally, the call server close () closes the socket (with statement, do not worry).

Guess you like

Origin www.cnblogs.com/daemonFlY/p/11495296.html