summaryrefslogtreecommitdiffstats
path: root/Doc/library/socketserver.rst
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-02-19 03:27:46 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-02-19 03:27:46 (GMT)
commiteac17b8955278619ec1db7c5e70748b3ac7b83fe (patch)
tree9d1675db43eb1be59810f9e1b08899da03c309f4 /Doc/library/socketserver.rst
parent4bf427495fc65f331ae11903b57f6aa712a35ee3 (diff)
downloadcpython-eac17b8955278619ec1db7c5e70748b3ac7b83fe.zip
cpython-eac17b8955278619ec1db7c5e70748b3ac7b83fe.tar.gz
cpython-eac17b8955278619ec1db7c5e70748b3ac7b83fe.tar.bz2
Issue #15608: Improve socketserver module documentation
* Add headings for each concrete and mix-in class and list methods and attributes under them * 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 * Document constructor parameters for the server classes * Remove version 2.6 not relevant for Python 3 documentation
Diffstat (limited to 'Doc/library/socketserver.rst')
-rw-r--r--Doc/library/socketserver.rst356
1 files changed, 202 insertions, 154 deletions
diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
index 18be936..f0cfc80 100644
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -10,16 +10,34 @@
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
-W. Richard Steven's UNIX Network Programming or Ralph Davis's Win32 Network
-Programming.
+There are four basic concrete server classes:
+
+
+.. class:: TCPServer(server_address, RequestHandlerClass, bind_and_activate=True)
+
+ This uses the Internet TCP protocol, which provides for
+ continuous streams of data between the client and server.
+ If *bind_and_activate* is true, the constructor automatically attempts to
+ invoke :meth:`~BaseServer.server_bind` and
+ :meth:`~BaseServer.server_activate`. The other parameters are passed to
+ the :class:`BaseServer` base class.
+
+
+.. class:: UDPServer(server_address, RequestHandlerClass, bind_and_activate=True)
+
+ This uses datagrams, which are discrete packets of information that may
+ arrive out of order or be lost while in transit. The parameters are
+ the same as for :class:`TCPServer`.
+
+
+.. class:: UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True)
+ UnixDatagramServer(server_address, RequestHandlerClass, bind_and_activate=True)
+
+ 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. The parameters are the same as for
+ :class:`TCPServer`.
+
These four classes process requests :dfn:`synchronously`; each request must be
completed before the next request can be started. This isn't suitable if each
@@ -31,10 +49,12 @@ support asynchronous behaviour.
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. Then 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. Finally, call :meth:`~BaseServer.server_close`
to close the socket.
@@ -76,18 +96,33 @@ Note that :class:`UnixDatagramServer` derives from :class:`UDPServer`, not from
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
+
+ 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.
-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.
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
@@ -109,7 +144,7 @@ has requested. Here a threading or forking server is appropriate.
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
@@ -127,227 +162,240 @@ connected for a long time (if threads or subprocesses cannot be used). See
Server Objects
--------------
-.. class:: BaseServer
+.. class:: 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.
+ done in subclasses. The two parameters are stored in the respective
+ :attr:`server_address` and :attr:`RequestHandlerClass` attributes.
+
+
+ .. method:: fileno()
+
+ Return an integer file descriptor for the socket on which the server is
+ listening. This function is most commonly passed to :mod:`selectors`, to
+ allow monitoring multiple servers in the same process.
-.. method:: BaseServer.fileno()
+ .. method:: handle_request()
- Return an integer file descriptor for the socket on which the server is
- listening. This function is most commonly passed to :mod:`selectors`, to
- allow monitoring multiple servers in the same process.
+ 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:`~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:`timeout`
+ seconds, :meth:`handle_timeout` will be called and :meth:`handle_request`
+ will return.
-.. method:: BaseServer.handle_request()
+ .. method:: serve_forever(poll_interval=0.5)
- 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
- handler class raises an exception, the server's :meth:`handle_error` method
- will be called. If no request is received within :attr:`self.timeout`
- seconds, :meth:`handle_timeout` will be called and :meth:`handle_request`
- will return.
+ Handle requests until an explicit :meth:`shutdown` request. Poll for
+ 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
+ child processes.
+ .. versionchanged:: 3.3
+ Added ``service_actions`` call to the ``serve_forever`` method.
-.. 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
- 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
- child processes.
+ .. method:: service_actions()
- .. versionchanged:: 3.3
- Added ``service_actions`` call to the ``serve_forever`` method.
+ This is called in the :meth:`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.
+ .. versionadded:: 3.3
-.. method:: BaseServer.service_actions()
+ .. method:: shutdown()
- This is called in the :meth:`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.
+ Tell the :meth:`serve_forever` loop to stop and wait until it does.
- .. versionadded:: 3.3
-.. method:: BaseServer.shutdown()
+ .. method:: server_close()
- Tell the :meth:`serve_forever` loop to stop and wait until it does.
+ Clean up the server. May be overridden.
-.. method:: BaseServer.server_close()
+ .. attribute:: address_family
- Clean up the server. May be overridden.
+ The family of protocols to which the server's socket belongs.
+ Common examples are :const:`socket.AF_INET` and :const:`socket.AF_UNIX`.
- .. versionadded:: 2.6
+ .. attribute:: RequestHandlerClass
-.. attribute:: BaseServer.address_family
+ The user-provided request handler class; an instance of this class is created
+ for each request.
- The family of protocols to which the server's socket belongs.
- Common examples are :const:`socket.AF_INET` and :const:`socket.AF_UNIX`.
+ .. attribute:: server_address
-.. attribute:: BaseServer.RequestHandlerClass
+ The address on which the server is listening. The format of addresses varies
+ depending on the protocol family;
+ see the documentation for the :mod:`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.
- The user-provided request handler class; an instance of this class is created
- for each request.
+ .. attribute:: socket
-.. attribute:: BaseServer.server_address
+ The socket object on which the server will listen for incoming requests.
- 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.
+ The server classes support the following class variables:
-.. attribute:: BaseServer.socket
+ .. XXX should class variables be covered before instance variables, or vice versa?
- The socket object on which the server will listen for incoming requests.
+ .. attribute:: allow_reuse_address
+ Whether the server will allow the reuse of an address. This defaults to
+ :const:`False`, and can be set in subclasses to change the policy.
-The server classes support the following class variables:
-.. XXX should class variables be covered before instance variables, or vice versa?
+ .. attribute:: request_queue_size
-.. attribute:: BaseServer.allow_reuse_address
+ 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 :attr:`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.
- Whether the server will allow the reuse of an address. This defaults to
- :const:`False`, and can be set in subclasses to change the policy.
+ .. attribute:: socket_type
-.. attribute:: BaseServer.request_queue_size
+ The type of socket used by the server; :const:`socket.SOCK_STREAM` and
+ :const:`socket.SOCK_DGRAM` are two common values.
- 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 :attr:`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.
+ .. attribute:: timeout
-.. attribute:: BaseServer.socket_type
+ Timeout duration, measured in seconds, or :const:`None` if no timeout is
+ desired. If :meth:`handle_request` receives no incoming requests within the
+ timeout period, the :meth:`handle_timeout` method is called.
- The type of socket used by the server; :const:`socket.SOCK_STREAM` and
- :const:`socket.SOCK_DGRAM` are two common values.
+ There are various server methods that can be overridden by subclasses of base
+ server classes like :class:`TCPServer`; these methods aren't useful to external
+ users of the server object.
-.. attribute:: BaseServer.timeout
+ .. XXX should the default implementations of these be documented, or should
+ it be assumed that the user will look at socketserver.py?
- Timeout duration, measured in seconds, or :const:`None` if no timeout is
- desired. If :meth:`handle_request` receives no incoming requests within the
- timeout period, the :meth:`handle_timeout` method is called.
+ .. method:: finish_request()
+ Actually processes the request by instantiating :attr:`RequestHandlerClass` and
+ calling its :meth:`~BaseRequestHandler.handle` method.
-There are various server methods that can be overridden by subclasses of base
-server classes like :class:`TCPServer`; these methods aren't useful to external
-users of the server object.
-.. XXX should the default implementations of these be documented, or should
- it be assumed that the user will look at socketserver.py?
+ .. method:: get_request()
-.. method:: BaseServer.finish_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.
- Actually processes the request by instantiating :attr:`RequestHandlerClass` and
- calling its :meth:`handle` method.
+ .. method:: handle_error(request, client_address)
-.. method:: BaseServer.get_request()
+ This function is called if the :meth:`~BaseRequestHandler.handle`
+ method of a :attr:`RequestHandlerClass` instance raises
+ an exception. The default action is to print the traceback to
+ standard output and continue handling further requests.
- 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.
+ .. method:: handle_timeout()
-.. method:: BaseServer.handle_error(request, client_address)
+ This function is called when the :attr:`timeout` attribute has been set to a
+ value other than :const:`None` 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.
- This function is called if the :attr:`RequestHandlerClass`'s :meth:`handle`
- method raises an exception. The default action is to print the traceback to
- standard output and continue handling further requests.
+ .. method:: process_request(request, client_address)
-.. method:: BaseServer.handle_timeout()
+ Calls :meth:`finish_request` to create an instance of the
+ :attr:`RequestHandlerClass`. If desired, this function can create a new process
+ or thread to handle the request; the :class:`ForkingMixIn` and
+ :class:`ThreadingMixIn` classes do this.
- This function is called when the :attr:`timeout` attribute has been set to a
- value other than :const:`None` 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.
+ .. Is there any point in documenting the following two functions?
+ What would the purpose of overriding them be: initializing server
+ instance variables, adding new network families?
-.. method:: BaseServer.process_request(request, client_address)
+ .. method:: server_activate()
- Calls :meth:`finish_request` to create an instance of the
- :attr:`RequestHandlerClass`. If desired, this function can create a new process
- or thread to handle the request; the :class:`ForkingMixIn` and
- :class:`ThreadingMixIn` classes do this.
+ Called by the server's constructor to activate the server. The default behavior
+ for a TCP server just invokes :meth:`~socket.socket.listen`
+ on the server's socket. May be overridden.
-.. Is there any point in documenting the following two functions?
- What would the purpose of overriding them be: initializing server
- instance variables, adding new network families?
+ .. method:: server_bind()
-.. method:: BaseServer.server_activate()
+ Called by the server's constructor to bind the socket to the desired address.
+ May be overridden.
- 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.
+ .. method:: verify_request(request, client_address)
-.. method:: BaseServer.server_bind()
+ Must return a Boolean value; if the value is :const:`True`, the request will
+ be processed, and if it's :const:`False`, the request will be denied. This
+ function can be overridden to implement access controls for a server. The
+ default implementation always returns :const:`True`.
- Called by the server's constructor to bind the socket to the desired address.
- May be overridden.
+Request Handler Objects
+-----------------------
-.. method:: BaseServer.verify_request(request, client_address)
+.. class:: BaseRequestHandler
- Must return a Boolean value; if the value is :const:`True`, the request will
- be processed, and if it's :const:`False`, the request will be denied. This
- function can be overridden to implement access controls for a server. The
- default implementation always returns :const:`True`.
+ 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.
-RequestHandler Objects
-----------------------
+ .. method:: setup()
-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.
+ Called before the :meth:`handle` method to perform any initialization actions
+ required. The default implementation does nothing.
-.. method:: RequestHandler.finish()
+ .. method:: handle()
- 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.
+ This function must do all the work required to service a request. The
+ default implementation does nothing. Several instance attributes are
+ available to it; the request is available as :attr:`self.request`; the client
+ address as :attr:`self.client_address`; and the server instance as
+ :attr:`self.server`, in case it needs access to per-server information.
+ 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.
-.. method:: RequestHandler.handle()
- This function must do all the work required to service a request. The
- default implementation does nothing. Several instance attributes are
- available to it; the request is available as :attr:`self.request`; the client
- address as :attr:`self.client_address`; and the server instance as
- :attr:`self.server`, in case it needs access to per-server information.
+ .. method:: finish()
- 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.
+ 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:: RequestHandler.setup()
+.. class:: StreamRequestHandler
+ DatagramRequestHandler
- Called before the :meth:`handle` method to perform any initialization actions
- required. The default implementation does nothing.
+ These :class:`BaseRequestHandler` 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
@@ -362,7 +410,7 @@ This is the server side::
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