18.18.2 Server Objects

fileno( )
Return an integer file descriptor for the socket on which the server is listening. This function is most commonly passed to select.select(), 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(), and process_request(). If the user-provided handle() method of the handler class raises an exception, the server's handle_error() method will be called.

serve_forever( )
Handle an infinite number of requests. This simply calls handle_request() inside an infinite loop.

The family of protocols to which the server's socket belongs. socket.AF_INET and socket.AF_UNIX are two possible values.

The user-provided request handler class; an instance of this class is created for each request.

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: ('', 80), for example.

The socket object on which the server will listen for incoming requests.

The server classes support the following class variables:

Whether the server will allow the reuse of an address. This defaults to False, and can be set in subclasses to change the policy.

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.

The type of socket used by the server; socket.SOCK_STREAM and socket.SOCK_DGRAM are two possible values.

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 its handle() 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 RequestHandlerClass's handle() method raises an exception. The default action is to print the traceback to standard output and continue handling further requests.

process_request( request, client_address)
Calls finish_request() to create an instance of the RequestHandlerClass. If desired, this function can create a new process or thread to handle the request; the ForkingMixIn and ThreadingMixIn classes do this.

server_activate( )
Called by the server's constructor to activate the server. The default behavior just listens to 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.

verify_request( request, client_address)
Must return a Boolean value; if the value is True, the request will be processed, and if it's False, the request will be denied. This function can be overridden to implement access controls for a server. The default implementation always returns True.

See About this document... for information on suggesting changes.