One: Socketserver simplifies the writing of web Servers.
It has 4 classes: tcpserver,udpserver,unixstreamserver,unixdatagramserver.
These 4 classes are processed synchronously, in addition to the Forkingmixin and ThreadingMixIn classes to support asynchronous
socketserver.
class TCPServer
(server_address, requesthandlerclass, bind_and_activate=true)
socketserver.
class UDPServer
(server_address, requesthandlerclass, bind_and_activate=true)
socketserver.
class UnixStreamServer
(server_address, requesthandlerclass, bind_and_activate=true)
socketserver.
class UnixDatagramServer
(server_address, requesthandlerclass,bind_and_activate=true)
Classsocketserver.
ForkingTCPServer
Classsocketserver.
ForkingUDPServer
Classsocketserver.
ThreadingTCPServer
Classsocketserver.
ThreadingUDPServer
Two: There is an inheritance relationship between five classes
+------------+| baseserver | +------------+ | v+-----------+ +------------------+| tcpserver |------->| unixstreamserver | +-----------+ +------------------+ | v+-----------+ +--------------------+| udpserver |------->| unixdatagramserver | +-----------+ +--------------------+
Unixstreamserver inheritance- tcpserver inheritance Baseserver
Inheritance Udpserver Inheritance TCPServer Inheritance Baseserver
Three: Use steps
1) You must create a request class yourself and inheritBaseRequestHandler,并重构hanndle方法
2)你必须实例化TCPServer,并且传递server ip和上面创建的请求的类
3)调用handle_request()方法或者server_forever()方法,
handle_request,只处理一个请求,
server_forever 处理N多请求
4)关闭call_server()
创建服务器的步骤。首先,你必须创建一个请求处理类,它是BaseRequestHandler的子类并重载其handle()方法。
其次,你必须实例化一个服务器类,传入服务器的地址和请求处理程序类。
最后,调用handle_request()(一般是调用其他事件循环或者使用select())或serve_forever()。
Four: Baseserver
Class Socketserver.baseserver: This is the superclass of all the server objects in the Module. It defines the interface, as described below, but most of the methods are not implemented and are refined in Subclasses.
Baseserver.handle_request (): processes a single Request. Processing order: get_request (), verify_request (), process_request (). If the user provides the handle () method throws an exception, the Server's handle_error () method is Called. If no request is received within the self.timeout, the Handle_timeout () is called and the Handle_request () is Returned.
The server class supports the following class Variables:
The size of the request Queue. If a single request takes a long time to process, the server is busy when the request is placed in the queue, up to a maximum of request_queue_size. Once the queue is full, requests from the client will get a "Connection denied" Error. The default value is typically 5, but can be overridden by a quilt class.
Baseserver.timeout: timeout, in seconds, or none means no Time-out. If Handle_request () does not receive a request within a timeout, handle_timeout () is Called.
The following methods can be overridden by subclasses, which have no effect on external users of server Objects.
Baseserver.process_request (request, client_address): Call Finish_request () to create an instance of Requesthandlerclass. If required, This feature can create new processes or threads to handle requests, which the Forkingmixin and ThreadingMixIn classes do. Common.
Baseserver.verify_request (request, client_address): Returns a Boolean value that, if true, will be processed and the request will be Rejected. This feature can be overridden to implement access control to the Server. The default implementation always returns True. The client_address can restrict the client, such as the request to handle only the specified IP range. Common.
Request processor
The processor receives the data and decides what to do. It is responsible for implementing protocols on top of the socket Layer (i.e., HTTP, xml-rpc, or AMQP), reading data, processing, and writing Reactions. The following methods can be overloaded:
Setup (): prepares request Processing. By default, Nothing is done, and Streamrequesthandler creates a File-like object to read and write to the Socket.
Handle (): Process the Request. Resolves incoming requests, processes data, and sends a response. Nothing is done by Default. Common Variables: Self.request,self.client_address,self.server.
Finish (): Environment Cleanup. Nothing is done by default, and if Setup produces an exception, the finish is not Executed.
typically, you only need to overload Handle. The type of self.request differs from the service of the datagram or Stream. For streaming services, Self.request is the socket object; for datagram services, self.request is a string and a socket. You can overload in subclasses Streamrequesthandler or datagramrequesthandler, rewrite setup () and finish (), and provide self.rfile and Self.wfile Properties. Self.rfile and Self.wfile can read or write to obtain the requested data or return the data to the Client.
Manipulating the Python basic learning log Day8-socketserver