socketserver --- 用於網路伺服器的框架¶
socketserver 模組簡化了編寫網路伺服器的任務。
可用性: not WASI.
此模組在 WebAssembly 平台上不起作用或無法使用。更多資訊請參閱 WebAssembly 平台。
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. It is recommended to use
the server in a with statement. 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 (unless you used a with statement).
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.
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
server is the address family.
- 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().- block_on_close¶
ForkingMixIn.server_closewaits until all child processes complete, except ifblock_on_closeattribute isFalse.ThreadingMixIn.server_closewaits until all non-daemon threads complete, except ifblock_on_closeattribute isFalse.
- max_children¶
Specify how many child processes will exist to handle requests at a time for
ForkingMixIn. If the limit is reached, new requests will wait until one child process has finished.
- daemon_threads¶
For
ThreadingMixInuse daemonic threads by settingThreadingMixIn.daemon_threadstoTrueto not wait until threads complete.
在 3.7 版的變更:
ForkingMixIn.server_closeandThreadingMixIn.server_closenow waits until all child processes and non-daemonic threads complete. Add a newForkingMixIn.block_on_closeclass attribute to opt-in for the pre-3.7 behaviour.
- class socketserver.ForkingTCPServer¶
- class socketserver.ForkingUDPServer¶
- class socketserver.ThreadingTCPServer¶
- class socketserver.ThreadingUDPServer¶
- class socketserver.ForkingUnixStreamServer¶
- class socketserver.ForkingUnixDatagramServer¶
- class socketserver.ThreadingUnixStreamServer¶
- class socketserver.ThreadingUnixDatagramServer¶
These classes are pre-defined using the mix-in classes.
在 3.12 版被加入: The ForkingUnixStreamServer and ForkingUnixDatagramServer classes
were added.
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 selectors 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).
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
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 withintimeoutseconds,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 the