source: python/trunk/Doc/library/socketserver.rst

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 20.1 KB
RevLine 
[2]1:mod:`SocketServer` --- A framework for network servers
2=======================================================
3
4.. module:: SocketServer
5 :synopsis: A framework for network servers.
6
7.. note::
8
9 The :mod:`SocketServer` module has been renamed to :mod:`socketserver` in
[391]10 Python 3. The :term:`2to3` tool will automatically adapt imports when
11 converting your sources to Python 3.
[2]12
[391]13**Source code:** :source:`Lib/SocketServer.py`
[2]14
[391]15--------------
16
[2]17The :mod:`SocketServer` module simplifies the task of writing network servers.
18
19There are four basic server classes: :class:`TCPServer` uses the Internet TCP
20protocol, which provides for continuous streams of data between the client and
21server. :class:`UDPServer` uses datagrams, which are discrete packets of
22information that may arrive out of order or be lost while in transit. The more
23infrequently used :class:`UnixStreamServer` and :class:`UnixDatagramServer`
24classes are similar, but use Unix domain sockets; they're not available on
25non-Unix platforms. For more details on network programming, consult a book
26such as
27W. Richard Steven's UNIX Network Programming or Ralph Davis's Win32 Network
28Programming.
29
30These four classes process requests :dfn:`synchronously`; each request must be
31completed before the next request can be started. This isn't suitable if each
32request takes a long time to complete, because it requires a lot of computation,
33or because it returns a lot of data which the client is slow to process. The
34solution is to create a separate process or thread to handle each request; the
35:class:`ForkingMixIn` and :class:`ThreadingMixIn` mix-in classes can be used to
36support asynchronous behaviour.
37
38Creating a server requires several steps. First, you must create a request
39handler class by subclassing the :class:`BaseRequestHandler` class and
40overriding its :meth:`handle` method; this method will process incoming
41requests. Second, you must instantiate one of the server classes, passing it
42the server's address and the request handler class. Finally, call the
43:meth:`handle_request` or :meth:`serve_forever` method of the server object to
44process one or many requests.
45
46When inheriting from :class:`ThreadingMixIn` for threaded connection behavior,
47you should explicitly declare how you want your threads to behave on an abrupt
48shutdown. The :class:`ThreadingMixIn` class defines an attribute
49*daemon_threads*, which indicates whether or not the server should wait for
50thread termination. You should set the flag explicitly if you would like threads
51to behave autonomously; the default is :const:`False`, meaning that Python will
52not exit until all threads created by :class:`ThreadingMixIn` have exited.
53
54Server classes have the same external methods and attributes, no matter what
55network protocol they use.
56
57
58Server Creation Notes
59---------------------
60
61There are five classes in an inheritance diagram, four of which represent
62synchronous servers of four types::
63
64 +------------+
65 | BaseServer |
66 +------------+
67 |
68 v
69 +-----------+ +------------------+
70 | TCPServer |------->| UnixStreamServer |
71 +-----------+ +------------------+
72 |
73 v
74 +-----------+ +--------------------+
75 | UDPServer |------->| UnixDatagramServer |
76 +-----------+ +--------------------+
77
78Note that :class:`UnixDatagramServer` derives from :class:`UDPServer`, not from
79:class:`UnixStreamServer` --- the only difference between an IP and a Unix
80stream server is the address family, which is simply repeated in both Unix
81server classes.
82
83Forking and threading versions of each type of server can be created using the
84:class:`ForkingMixIn` and :class:`ThreadingMixIn` mix-in classes. For instance,
85a threading UDP server class is created as follows::
86
87 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
88
89The mix-in class must come first, since it overrides a method defined in
[391]90:class:`UDPServer`. Setting the various attributes also change the
[2]91behavior of the underlying server mechanism.
92
93To implement a service, you must derive a class from :class:`BaseRequestHandler`
94and redefine its :meth:`handle` method. You can then run various versions of
95the service by combining one of the server classes with your request handler
96class. The request handler class must be different for datagram or stream
97services. This can be hidden by using the handler subclasses
98:class:`StreamRequestHandler` or :class:`DatagramRequestHandler`.
99
100Of course, you still have to use your head! For instance, it makes no sense to
101use a forking server if the service contains state in memory that can be
102modified by different requests, since the modifications in the child process
103would never reach the initial state kept in the parent process and passed to
104each child. In this case, you can use a threading server, but you will probably
105have to use locks to protect the integrity of the shared data.
106
107On the other hand, if you are building an HTTP server where all data is stored
108externally (for instance, in the file system), a synchronous class will
109essentially render the service "deaf" while one request is being handled --
110which may be for a very long time if a client is slow to receive all the data it
111has requested. Here a threading or forking server is appropriate.
112
113In some cases, it may be appropriate to process part of a request synchronously,
114but to finish processing in a forked child depending on the request data. This
115can be implemented by using a synchronous server and doing an explicit fork in
116the request handler class :meth:`handle` method.
117
118Another approach to handling multiple simultaneous requests in an environment
[391]119that supports neither threads nor :func:`~os.fork` (or where these are too
120expensive or inappropriate for the service) is to maintain an explicit table of
121partially finished requests and to use :func:`~select.select` to decide which
122request to work on next (or whether to handle a new incoming request). This is
123particularly important for stream services where each client can potentially be
124connected for a long time (if threads or subprocesses cannot be used). See
125:mod:`asyncore` for another way to manage this.
[2]126
127.. XXX should data and methods be intermingled, or separate?
128 how should the distinction between class and instance variables be drawn?
129
130
131Server Objects
132--------------
133
134.. class:: BaseServer
135
136 This is the superclass of all Server objects in the module. It defines the
137 interface, given below, but does not implement most of the methods, which is
138 done in subclasses.
139
140
141.. method:: BaseServer.fileno()
142
143 Return an integer file descriptor for the socket on which the server is
144 listening. This function is most commonly passed to :func:`select.select`, to
145 allow monitoring multiple servers in the same process.
146
147
148.. method:: BaseServer.handle_request()
149
150 Process a single request. This function calls the following methods in
151 order: :meth:`get_request`, :meth:`verify_request`, and
152 :meth:`process_request`. If the user-provided :meth:`handle` method of the
153 handler class raises an exception, the server's :meth:`handle_error` method
154 will be called. If no request is received within :attr:`self.timeout`
155 seconds, :meth:`handle_timeout` will be called and :meth:`handle_request`
156 will return.
157
158
159.. method:: BaseServer.serve_forever(poll_interval=0.5)
160
[391]161 Handle requests until an explicit :meth:`shutdown` request.
162 Poll for shutdown every *poll_interval* seconds. Ignores :attr:`self.timeout`.
163 If you need to do periodic tasks, do them in another thread.
[2]164
165
166.. method:: BaseServer.shutdown()
167
[391]168 Tell the :meth:`serve_forever` loop to stop and wait until it does.
[2]169
170 .. versionadded:: 2.6
171
172
173.. attribute:: BaseServer.address_family
174
175 The family of protocols to which the server's socket belongs.
176 Common examples are :const:`socket.AF_INET` and :const:`socket.AF_UNIX`.
177
178
179.. attribute:: BaseServer.RequestHandlerClass
180
181 The user-provided request handler class; an instance of this class is created
182 for each request.
183
184
185.. attribute:: BaseServer.server_address
186
187 The address on which the server is listening. The format of addresses varies
188 depending on the protocol family; see the documentation for the socket module
189 for details. For Internet protocols, this is a tuple containing a string giving
190 the address, and an integer port number: ``('127.0.0.1', 80)``, for example.
191
192
193.. attribute:: BaseServer.socket
194
195 The socket object on which the server will listen for incoming requests.
196
197
198The server classes support the following class variables:
199
200.. XXX should class variables be covered before instance variables, or vice versa?
201
202.. attribute:: BaseServer.allow_reuse_address
203
204 Whether the server will allow the reuse of an address. This defaults to
205 :const:`False`, and can be set in subclasses to change the policy.
206
207
208.. attribute:: BaseServer.request_queue_size
209
210 The size of the request queue. If it takes a long time to process a single
211 request, any requests that arrive while the server is busy are placed into a
212 queue, up to :attr:`request_queue_size` requests. Once the queue is full,
213 further requests from clients will get a "Connection denied" error. The default
214 value is usually 5, but this can be overridden by subclasses.
215
216
217.. attribute:: BaseServer.socket_type
218
219 The type of socket used by the server; :const:`socket.SOCK_STREAM` and
220 :const:`socket.SOCK_DGRAM` are two common values.
221
222
223.. attribute:: BaseServer.timeout
224
225 Timeout duration, measured in seconds, or :const:`None` if no timeout is
226 desired. If :meth:`handle_request` receives no incoming requests within the
227 timeout period, the :meth:`handle_timeout` method is called.
228
229
230There are various server methods that can be overridden by subclasses of base
231server classes like :class:`TCPServer`; these methods aren't useful to external
232users of the server object.
233
234.. XXX should the default implementations of these be documented, or should
235 it be assumed that the user will look at SocketServer.py?
236
237.. method:: BaseServer.finish_request()
238
239 Actually processes the request by instantiating :attr:`RequestHandlerClass` and
240 calling its :meth:`handle` method.
241
242
243.. method:: BaseServer.get_request()
244
245 Must accept a request from the socket, and return a 2-tuple containing the *new*
246 socket object to be used to communicate with the client, and the client's
247 address.
248
249
250.. method:: BaseServer.handle_error(request, client_address)
251
252 This function is called if the :attr:`RequestHandlerClass`'s :meth:`handle`
253 method raises an exception. The default action is to print the traceback to
254 standard output and continue handling further requests.
255
256
257.. method:: BaseServer.handle_timeout()
258
259 This function is called when the :attr:`timeout` attribute has been set to a
260 value other than :const:`None` and the timeout period has passed with no
261 requests being received. The default action for forking servers is
262 to collect the status of any child processes that have exited, while
263 in threading servers this method does nothing.
264
265
266.. method:: BaseServer.process_request(request, client_address)
267
268 Calls :meth:`finish_request` to create an instance of the
269 :attr:`RequestHandlerClass`. If desired, this function can create a new process
270 or thread to handle the request; the :class:`ForkingMixIn` and
271 :class:`ThreadingMixIn` classes do this.
272
273
274.. Is there any point in documenting the following two functions?
275 What would the purpose of overriding them be: initializing server
276 instance variables, adding new network families?
277
278.. method:: BaseServer.server_activate()
279
280 Called by the server's constructor to activate the server. The default behavior
281 just :meth:`listen`\ s to the server's socket. May be overridden.
282
283
284.. method:: BaseServer.server_bind()
285
286 Called by the server's constructor to bind the socket to the desired address.
287 May be overridden.
288
289
290.. method:: BaseServer.verify_request(request, client_address)
291
292 Must return a Boolean value; if the value is :const:`True`, the request will be
293 processed, and if it's :const:`False`, the request will be denied. This function
294 can be overridden to implement access controls for a server. The default
295 implementation always returns :const:`True`.
296
297
298RequestHandler Objects
299----------------------
300
301The request handler class must define a new :meth:`handle` method, and can
302override any of the following methods. A new instance is created for each
303request.
304
305
306.. method:: RequestHandler.finish()
307
308 Called after the :meth:`handle` method to perform any clean-up actions
[391]309 required. The default implementation does nothing. If :meth:`setup`
310 raises an exception, this function will not be called.
[2]311
312
313.. method:: RequestHandler.handle()
314
315 This function must do all the work required to service a request. The
316 default implementation does nothing. Several instance attributes are
317 available to it; the request is available as :attr:`self.request`; the client
318 address as :attr:`self.client_address`; and the server instance as
319 :attr:`self.server`, in case it needs access to per-server information.
320
321 The type of :attr:`self.request` is different for datagram or stream
322 services. For stream services, :attr:`self.request` is a socket object; for
323 datagram services, :attr:`self.request` is a pair of string and socket.
324 However, this can be hidden by using the request handler subclasses
325 :class:`StreamRequestHandler` or :class:`DatagramRequestHandler`, which
326 override the :meth:`setup` and :meth:`finish` methods, and provide
327 :attr:`self.rfile` and :attr:`self.wfile` attributes. :attr:`self.rfile` and
328 :attr:`self.wfile` can be read or written, respectively, to get the request
329 data or return data to the client.
330
331
332.. method:: RequestHandler.setup()
333
334 Called before the :meth:`handle` method to perform any initialization actions
335 required. The default implementation does nothing.
336
337
338Examples
339--------
340
341:class:`SocketServer.TCPServer` Example
342~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
343
344This is the server side::
345
346 import SocketServer
347
348 class MyTCPHandler(SocketServer.BaseRequestHandler):
349 """
350 The RequestHandler class for our server.
351
352 It is instantiated once per connection to the server, and must
353 override the handle() method to implement communication to the
354 client.
355 """
356
357 def handle(self):
358 # self.request is the TCP socket connected to the client
359 self.data = self.request.recv(1024).strip()
[391]360 print "{} wrote:".format(self.client_address[0])
[2]361 print self.data
362 # just send back the same data, but upper-cased
[391]363 self.request.sendall(self.data.upper())
[2]364
365 if __name__ == "__main__":
366 HOST, PORT = "localhost", 9999
367
368 # Create the server, binding to localhost on port 9999
369 server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
370
371 # Activate the server; this will keep running until you
372 # interrupt the program with Ctrl-C
373 server.serve_forever()
374
375An alternative request handler class that makes use of streams (file-like
376objects that simplify communication by providing the standard file interface)::
377
378 class MyTCPHandler(SocketServer.StreamRequestHandler):
379
380 def handle(self):
381 # self.rfile is a file-like object created by the handler;
382 # we can now use e.g. readline() instead of raw recv() calls
383 self.data = self.rfile.readline().strip()
[391]384 print "{} wrote:".format(self.client_address[0])
[2]385 print self.data
386 # Likewise, self.wfile is a file-like object used to write back
387 # to the client
388 self.wfile.write(self.data.upper())
389
390The difference is that the ``readline()`` call in the second handler will call
391``recv()`` multiple times until it encounters a newline character, while the
392single ``recv()`` call in the first handler will just return what has been sent
[391]393from the client in one ``sendall()`` call.
[2]394
395
396This is the client side::
397
398 import socket
399 import sys
400
401 HOST, PORT = "localhost", 9999
402 data = " ".join(sys.argv[1:])
403
404 # Create a socket (SOCK_STREAM means a TCP socket)
405 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
406
[391]407 try:
408 # Connect to server and send data
409 sock.connect((HOST, PORT))
410 sock.sendall(data + "\n")
[2]411
[391]412 # Receive data from the server and shut down
413 received = sock.recv(1024)
414 finally:
415 sock.close()
[2]416
[391]417 print "Sent: {}".format(data)
418 print "Received: {}".format(received)
[2]419
420
421The output of the example should look something like this:
422
423Server::
424
425 $ python TCPServer.py
426 127.0.0.1 wrote:
427 hello world with TCP
428 127.0.0.1 wrote:
429 python is nice
430
431Client::
432
433 $ python TCPClient.py hello world with TCP
434 Sent: hello world with TCP
435 Received: HELLO WORLD WITH TCP
436 $ python TCPClient.py python is nice
437 Sent: python is nice
438 Received: PYTHON IS NICE
439
440
441:class:`SocketServer.UDPServer` Example
442~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
443
444This is the server side::
445
446 import SocketServer
447
448 class MyUDPHandler(SocketServer.BaseRequestHandler):
449 """
450 This class works similar to the TCP handler class, except that
451 self.request consists of a pair of data and client socket, and since
452 there is no connection the client address must be given explicitly
453 when sending data back via sendto().
454 """
455
456 def handle(self):
457 data = self.request[0].strip()
458 socket = self.request[1]
[391]459 print "{} wrote:".format(self.client_address[0])
[2]460 print data
461 socket.sendto(data.upper(), self.client_address)
462
463 if __name__ == "__main__":
[391]464 HOST, PORT = "localhost", 9999
465 server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
466 server.serve_forever()
[2]467
468This is the client side::
469
470 import socket
471 import sys
472
473 HOST, PORT = "localhost", 9999
474 data = " ".join(sys.argv[1:])
475
476 # SOCK_DGRAM is the socket type to use for UDP sockets
477 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
478
479 # As you can see, there is no connect() call; UDP has no connections.
480 # Instead, data is directly sent to the recipient via sendto().
481 sock.sendto(data + "\n", (HOST, PORT))
482 received = sock.recv(1024)
483
[391]484 print "Sent: {}".format(data)
485 print "Received: {}".format(received)
[2]486
487The output of the example should look exactly like for the TCP server example.
488
489
490Asynchronous Mixins
491~~~~~~~~~~~~~~~~~~~
492
493To build asynchronous handlers, use the :class:`ThreadingMixIn` and
494:class:`ForkingMixIn` classes.
495
496An example for the :class:`ThreadingMixIn` class::
497
498 import socket
499 import threading
500 import SocketServer
501
502 class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
503
504 def handle(self):
505 data = self.request.recv(1024)
[391]506 cur_thread = threading.current_thread()
507 response = "{}: {}".format(cur_thread.name, data)
508 self.request.sendall(response)
[2]509
510 class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
511 pass
512
513 def client(ip, port, message):
514 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
515 sock.connect((ip, port))
[391]516 try:
517 sock.sendall(message)
518 response = sock.recv(1024)
519 print "Received: {}".format(response)
520 finally:
521 sock.close()
[2]522
523 if __name__ == "__main__":
524 # Port 0 means to select an arbitrary unused port
525 HOST, PORT = "localhost", 0
526
527 server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
528 ip, port = server.server_address
529
530 # Start a thread with the server -- that thread will then start one
531 # more thread for each request
532 server_thread = threading.Thread(target=server.serve_forever)
533 # Exit the server thread when the main thread terminates
[391]534 server_thread.daemon = True
[2]535 server_thread.start()
[391]536 print "Server loop running in thread:", server_thread.name
[2]537
538 client(ip, port, "Hello World 1")
539 client(ip, port, "Hello World 2")
540 client(ip, port, "Hello World 3")
541
542 server.shutdown()
543
[391]544
[2]545The output of the example should look something like this::
546
547 $ python ThreadedTCPServer.py
548 Server loop running in thread: Thread-1
549 Received: Thread-2: Hello World 1
550 Received: Thread-3: Hello World 2
551 Received: Thread-4: Hello World 3
552
553
554The :class:`ForkingMixIn` class is used in the same way, except that the server
555will spawn a new process for each request.
Note: See TracBrowser for help on using the repository browser.