class socketserver.BaseServer(server_address, RequestHandlerClass)
This is the superclass of all Server objects in the module. It defines the interface, given below, but does not implement most of the methods, which is done in subclasses. The two parameters are stored in the respective server_address
and RequestHandlerClass
attributes.
-
fileno()
-
Return an integer file descriptor for the socket on which the server is listening. This function is most commonly passed to
selectors
, to allow monitoring multiple servers in the same process.
-
handle_request()
-
Process a single request. This function calls the following methods in order:
get_request()
,verify_request()
, andprocess_request()
. If the user-providedhandle()
method of the handler class raises an exception, the server’shandle_error()
method will be called. If no request is received withintimeout
seconds,handle_timeout()
will be called andhandle_request()
will return.
-
serve_forever(poll_interval=0.5)
-
Handle requests until an explicit
shutdown()
request. Poll for shutdown every poll_interval seconds. Ignores thetimeout
attribute. It also callsservice_actions()
, which may be used by a subclass or mixin to provide actions specific to a given service. For example, theForkingMixIn
class usesservice_actions()
to clean up zombie child processes.Changed in version 3.3: Added
service_actions
call to theserve_forever
method.
-
service_actions()
-
This is called in the
serve_forever()
loop. This method can be overridden by subclasses or mixin classes to perform actions specific to a given service, such as cleanup actions.New in version 3.3.
-
shutdown()
-
Tell the
serve_forever()
loop to stop and wait until it does.
-
server_close()
-
Clean up the server. May be overridden.
-
address_family
-
The family of protocols to which the server’s socket belongs. Common examples are
socket.AF_INET
andsocket.AF_UNIX
.
-
RequestHandlerClass
-
The user-provided request handler class; an instance of this class is created for each request.
-
server_address
-
The address on which the server is listening. The format of addresses varies depending on the protocol family; see the documentation for the
socket
module for details. For Internet protocols, this is a tuple containing a string giving the address, and an integer port number:('127.0.0.1', 80)
, for example.
-
socket
-
The socket object on which the server will listen for incoming requests.
The server classes support the following class variables:
-
allow_reuse_address
-
Whether the server will allow the reuse of an address. This defaults to
False
, and can be set in subclasses to change the policy.
-
request_queue_size
-
The size of the request queue. If it takes a long time to process a single request, any requests that arrive while the server is busy are placed into a queue, up to
request_queue_size
requests. Once the queue is full, further requests from clients will get a “Connection denied” error. The default value is usually 5, but this can be overridden by subclasses.
-
socket_type
-
The type of socket used by the server;
socket.SOCK_STREAM
andsocket.SOCK_DGRAM
are two common values.
-
timeout
-
Timeout duration, measured in seconds, or
None
if no timeout is desired. Ifhandle_request()
receives no incoming requests within the timeout period, thehandle_timeout()
method is called.
There are various server methods that can be overridden by subclasses of base server classes like TCPServer
; these methods aren’t useful to external users of the server object.
-
finish_request()
-
Actually processes the request by instantiating
RequestHandlerClass
and calling itshandle()
method.
-
get_request()
-
Must accept a request from the socket, and return a 2-tuple containing the new socket object to be used to communicate with the client, and the client’s address.
-
handle_error(request, client_address)
-
This function is called if the
handle()
method of aRequestHandlerClass
instance raises an exception. The default action is to print the traceback to standard output and continue handling further requests.
-
handle_timeout()
-
This function is called when the
timeout
attribute has been set to a value other thanNone
and the timeout period has passed with no requests being received. The default action for forking servers is to collect the status of any child processes that have exited, while in threading servers this method does nothing.
-
process_request(request, client_address)
-
Calls
finish_request()
to create an instance of theRequestHandlerClass
. If desired, this function can create a new process or thread to handle the request; theForkingMixIn
andThreadingMixIn
classes do this.
-
server_activate()
-
Called by the server’s constructor to activate the server. The default behavior for a TCP server just invokes
listen()
on the server’s socket. May be overridden.
-
server_bind()
-
Called by the server’s constructor to bind the socket to the desired address. May be overridden.
Please login to continue.