20.17. SocketServer — A framework for network servers¶
Note
The SocketServer module has been renamed to socketserver in
Python 3. The 2to3 tool will automatically adapt imports when
converting your sources to Python 3.
Source code: Lib/SocketServer.py
The SocketServer module simplifies the task of writing network servers.
There are four basic concrete server classes:
-
class
SocketServer.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
server_bind()andserver_activate(). The other parameters are passed to theBaseServerbase class.
-
class
SocketServer.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
TCPServer.
-
class
SocketServer.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True)¶ -
class
SocketServer.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
TCPServer.
These four classes process requests synchronously; each request must be
completed before the next request can be started. This isn’t suitable if each
request takes a long time to complete, because it requires a lot of computation,
or because it returns a lot of data which the client is slow to process. The
solution is to create a separate process or thread to handle each request; the
ForkingMixIn and ThreadingMixIn mix-in classes can be used to
support asynchronous behaviour.
Creating a server requires several steps. First, you must create a request
handler class by subclassing the BaseRequestHandler class and
overriding its 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
handle_request() or
serve_forever() method of the server object to
process one or many requests. Finally, call server_close()
to close the socket.
When inheriting from ThreadingMixIn for threaded connection behavior,
you should explicitly declare how you want your threads to behave on an abrupt
shutdown. The ThreadingMixIn class defines an attribute
daemon_threads, which indicates whether or not the server should wait for
thread termination. You should set the flag explicitly if you would like threads
to behave autonomously; the default is False, meaning that Python will
not exit until all threads created by ThreadingMixIn have exited.
Server classes have the same external methods and attributes, no matter what network protocol they use.
20.17.1. Server Creation Notes¶
There are five classes in an inheritance diagram, four of which represent synchronous servers of four types:
+------------+
| BaseServer |
+------------+
|
v
+-----------+ +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+ +------------------+
|
v
+-----------+ +--------------------+
| UDPServer |------->| UnixDatagramServer |
+-----------+ +--------------------+
Note that UnixDatagramServer derives from UDPServer, not from
UnixStreamServer — the only difference between an IP and a Unix
stream server is the address family, which is simply repeated in both Unix
server classes.
-
class
SocketServer.ForkingMixIn¶ -
class
SocketServer.ThreadingMixIn¶ Forking and threading versions of each type of server can be created using these mix-in classes. For instance,
ThreadingUDPServeris created as follows:class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
The mix-in class comes first, since it overrides a method defined in
UDPServer. Setting the various attributes also changes the behavior of the underlying server mechanism.ForkingMixInand the Forking classes mentioned below are only available on POSIX platforms that supportfork().
-
class
SocketServer.ForkingTCPServer¶ -
class
SocketServer.ForkingUDPServer¶ -
class
SocketServer.ThreadingTCPServer¶ -
class
SocketServer.ThreadingUDPServer¶ These classes are pre-defined using the mix-in classes.
To implement a service, you must derive a class from BaseRequestHandler
and redefine its 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
StreamRequestHandler or DatagramRequestHandler.
Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by different requests, since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child. In this case, you can use a threading server, but you will probably have to use locks to protect the integrity of the shared data.
On the other hand, if you are building an HTTP server where all data is stored externally (for instance, in the file system), a synchronous class will essentially render the service “deaf” while one request is being handled – which may be for a very long time if a client is slow to receive all the data it 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 handle() method.
Another approach to handling multiple simultaneous requests in an environment
that supports neither threads nor fork() (or where these are too
expensive or inappropriate for the service) is to maintain an explicit table of
partially finished requests and to use select() to decide which
request to work on next (or whether to handle a new incoming request). This is
particularly important for stream services where each client can potentially be
connected for a long time (if threads or subprocesses cannot be used). See
asyncore for another way to manage this.
20.17.2. Server Objects¶
-
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_addressandRequestHandlerClassattributes.-
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(),
-
