# HG changeset patch # Parent 469ff344f8fd13e98d8c104dff42d0c974b51576 socketserver module documentation * Add headings for each concrete and mix-in class * Fix class and method cross references * Changed RequestHandler to BaseRequestHandler and added class heading * Pull out Stream/DatagramRequestHandler definitions * Reordered the request handler setup(), handle(), finish() methods diff -r 469ff344f8fd Doc/library/socketserver.rst --- a/Doc/library/socketserver.rst Sat Jan 31 12:20:40 2015 -0800 +++ b/Doc/library/socketserver.rst Sun Feb 01 04:35:28 2015 +0000 @@ -10,14 +10,30 @@ The :mod:`socketserver` module simplifies the task of writing network servers. -There are four basic server classes: :class:`TCPServer` uses the Internet TCP -protocol, which provides for continuous streams of data between the client and -server. :class:`UDPServer` uses datagrams, which are discrete packets of -information that may arrive out of order or be lost while in transit. The more -infrequently used :class:`UnixStreamServer` and :class:`UnixDatagramServer` -classes are similar, but use Unix domain sockets; they're not available on -non-Unix platforms. For more details on network programming, consult a book -such as +There are four basic concrete server classes: + + +.. class:: TCPServer + + This uses the Internet TCP protocol, which provides for + continuous streams of data between the client and server. + + +.. class:: UDPServer + + This uses datagrams, which are discrete packets of information that may + arrive out of order or be lost while in transit. + + +.. class:: UnixStreamServer + UnixDatagramServer + + These more infrequently used classes are similar to the TCP and + UDP classes, but use Unix domain sockets; they're not available on + non-Unix platforms. + + +For more details on network programming, consult a book such as W. Richard Steven's UNIX Network Programming or Ralph Davis's Win32 Network Programming. @@ -31,10 +47,12 @@ Creating a server requires several steps. First, you must create a request handler class by subclassing the :class:`BaseRequestHandler` class and -overriding its :meth:`handle` method; this method will process incoming +overriding its :meth:`~BaseRequestHandler.handle` method; +this method will process incoming requests. Second, you must instantiate one of the server classes, passing it the server's address and the request handler class. Finally, call the -:meth:`handle_request` or :meth:`serve_forever` method of the server object to +:meth:`~BaseServer.handle_request` or +:meth:`~BaseServer.serve_forever` method of the server object to process one or many requests. When inheriting from :class:`ThreadingMixIn` for threaded connection behavior, @@ -75,18 +93,32 @@ stream server is the address family, which is simply repeated in both Unix server classes. -Forking and threading versions of each type of server can be created using the -:class:`ForkingMixIn` and :class:`ThreadingMixIn` mix-in classes. For instance, -a threading UDP server class is created as follows:: - class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass +.. class:: ForkingMixIn + ThreadingMixIn -The mix-in class must come first, since it overrides a method defined in -:class:`UDPServer`. Setting the various attributes also change the -behavior of the underlying server mechanism. + Forking and threading versions of each type of server can be created + using these mix-in classes. For instance, :class:`ThreadingUDPServer` + is created as follows:: + + class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass + + The mix-in class comes first, since it overrides a method defined in + :class:`UDPServer`. Setting the various attributes also changes the + behavior of the underlying server mechanism. + + +.. class:: ForkingTCPServer + ForkingUDPServer + ThreadingTCPServer + ThreadingUDPServer + + These classes are pre-defined using the mix-in classes. + To implement a service, you must derive a class from :class:`BaseRequestHandler` -and redefine its :meth:`handle` method. You can then run various versions of +and redefine its :meth:`~BaseRequestHandler.handle` method. +You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the handler subclasses @@ -108,7 +140,7 @@ In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in -the request handler class :meth:`handle` method. +the request handler class :meth:`~BaseRequestHandler.handle` method. Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor :func:`~os.fork` (or where these are too @@ -144,9 +176,10 @@ Process a single request. This function calls the following methods in order: :meth:`get_request`, :meth:`verify_request`, and - :meth:`process_request`. If the user-provided :meth:`handle` method of the + :meth:`process_request`. If the user-provided + :meth:`~BaseRequestHandler.handle` method of the handler class raises an exception, the server's :meth:`handle_error` method - will be called. If no request is received within :attr:`self.timeout` + will be called. If no request is received within :attr:`timeout` seconds, :meth:`handle_timeout` will be called and :meth:`handle_request` will return. @@ -154,7 +187,8 @@ .. method:: BaseServer.serve_forever(poll_interval=0.5) Handle requests until an explicit :meth:`shutdown` request. Poll for - shutdown every *poll_interval* seconds. Ignores :attr:`self.timeout`. It + shutdown every *poll_interval* seconds. + Ignores the :attr:`timeout` attribute. It also calls :meth:`service_actions`, which may be used by a subclass or mixin to provide actions specific to a given service. For example, the :class:`ForkingMixIn` class uses :meth:`service_actions` to clean up zombie @@ -244,7 +278,7 @@ .. method:: BaseServer.finish_request() Actually processes the request by instantiating :attr:`RequestHandlerClass` and - calling its :meth:`handle` method. + calling its :meth:`~BaseRequestHandler.handle` method. .. method:: BaseServer.get_request() @@ -256,7 +290,8 @@ .. method:: BaseServer.handle_error(request, client_address) - This function is called if the :attr:`RequestHandlerClass`'s :meth:`handle` + This function is called if the :attr:`RequestHandlerClass`'s + :meth:`~BaseRequestHandler.handle` method raises an exception. The default action is to print the traceback to standard output and continue handling further requests. @@ -285,7 +320,8 @@ .. method:: BaseServer.server_activate() Called by the server's constructor to activate the server. The default behavior - just :meth:`listen`\ s to the server's socket. May be overridden. + for a TCP server just invokes :meth:`~socket.socket.listen` + on the server's socket. May be overridden. .. method:: BaseServer.server_bind() @@ -302,22 +338,25 @@ default implementation always returns :const:`True`. -RequestHandler Objects ----------------------- +Request Handler Objects +----------------------- -The request handler class must define a new :meth:`handle` method, and can -override any of the following methods. A new instance is created for each -request. +.. class:: BaseRequestHandler + This is the superclass of all request handler objects. It defines + the interface, given below. A concrete request handler subclass must + define a new :meth:`handle` method, and can override any of + the other methods. A new instance of the subclass is created for each + request. -.. method:: RequestHandler.finish() - Called after the :meth:`handle` method to perform any clean-up actions - required. The default implementation does nothing. If :meth:`setup` - raises an exception, this function will not be called. +.. method:: BaseRequestHandler.setup() + Called before the :meth:`handle` method to perform any initialization actions + required. The default implementation does nothing. -.. method:: RequestHandler.handle() + +.. method:: BaseRequestHandler.handle() This function must do all the work required to service a request. The default implementation does nothing. Several instance attributes are @@ -328,18 +367,24 @@ The type of :attr:`self.request` is different for datagram or stream services. For stream services, :attr:`self.request` is a socket object; for datagram services, :attr:`self.request` is a pair of string and socket. - However, this can be hidden by using the request handler subclasses - :class:`StreamRequestHandler` or :class:`DatagramRequestHandler`, which - override the :meth:`setup` and :meth:`finish` methods, and provide - :attr:`self.rfile` and :attr:`self.wfile` attributes. :attr:`self.rfile` and - :attr:`self.wfile` can be read or written, respectively, to get the request - data or return data to the client. -.. method:: RequestHandler.setup() +.. method:: BaseRequestHandler.finish() - Called before the :meth:`handle` method to perform any initialization actions - required. The default implementation does nothing. + Called after the :meth:`handle` method to perform any clean-up actions + required. The default implementation does nothing. If :meth:`setup` + raises an exception, this function will not be called. + + +.. class:: StreamRequestHandler + DatagramRequestHandler + + These request handler subclasses override the + :meth:`~BaseRequestHandler.setup` and :meth:`~BaseRequestHandler.finish` + methods, and provide :attr:`self.rfile` and :attr:`self.wfile` attributes. + The :attr:`self.rfile` and :attr:`self.wfile` attributes can be + read or written, respectively, to get the request data or return data + to the client. Examples @@ -354,7 +399,7 @@ class MyTCPHandler(socketserver.BaseRequestHandler): """ - The RequestHandler class for our server. + The request handler class for our server. It is instantiated once per connection to the server, and must override the handle() method to implement communication to the