Mercurial > cpython-lru_cache-weakref
annotate Lib/SocketServer.py @ 49987:54c19a1bbabd 2.6
Post-2.6.1 tweak
| author | Barry Warsaw <barry@python.org> |
|---|---|
| date | Fri, 05 Dec 2008 05:03:06 +0000 |
| parents | ba1fa511b72d |
| children | fa080c4f539a |
| rev | line source |
|---|---|
| 2887 | 1 """Generic socket server classes. |
| 2 | |
| 3 This module tries to capture the various aspects of defining a server: | |
| 4 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
5 For socket-based servers: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
6 |
| 2887 | 7 - address family: |
|
18825
40900620293e
Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
Martin v. Löwis <martin@v.loewis.de>
parents:
18471
diff
changeset
|
8 - AF_INET{,6}: IP (Internet Protocol) sockets (default) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
9 - AF_UNIX: Unix domain sockets |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
10 - others, e.g. AF_DECNET are conceivable (see <socket.h> |
| 2887 | 11 - socket type: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
12 - SOCK_STREAM (reliable stream, e.g. TCP) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
13 - SOCK_DGRAM (datagrams, e.g. UDP) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
14 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
15 For request-based servers (including socket-based): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
16 |
| 2887 | 17 - client address verification before further looking at the request |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
18 (This is actually a hook for any processing that needs to look |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
19 at the request before anything else, e.g. logging) |
| 2887 | 20 - how to handle multiple requests: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
21 - synchronous (one request is handled at a time) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
22 - forking (each request is handled by a new process) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
23 - threading (each request is handled by a new thread) |
| 2887 | 24 |
| 25 The classes in this module favor the server type that is simplest to | |
| 26 write: a synchronous TCP/IP server. This is bad class design, but | |
| 27 save some typing. (There's also the issue that a deep class hierarchy | |
| 28 slows down method lookups.) | |
| 29 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
30 There are five classes in an inheritance diagram, four of which represent |
| 2887 | 31 synchronous servers of four types: |
| 32 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
33 +------------+ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
34 | BaseServer | |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
35 +------------+ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
36 | |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
37 v |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
38 +-----------+ +------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
39 | TCPServer |------->| UnixStreamServer | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
40 +-----------+ +------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
41 | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
42 v |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
43 +-----------+ +--------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
44 | UDPServer |------->| UnixDatagramServer | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
45 +-----------+ +--------------------+ |
| 2887 | 46 |
|
5803
b56d527a433f
Fix bug found by kjpylint; change doc string to avoid "(" in column 1.
Guido van Rossum <guido@python.org>
parents:
4459
diff
changeset
|
47 Note that UnixDatagramServer derives from UDPServer, not from |
| 2887 | 48 UnixStreamServer -- the only difference between an IP and a Unix |
| 49 stream server is the address family, which is simply repeated in both | |
|
5803
b56d527a433f
Fix bug found by kjpylint; change doc string to avoid "(" in column 1.
Guido van Rossum <guido@python.org>
parents:
4459
diff
changeset
|
50 unix server classes. |
| 2887 | 51 |
| 52 Forking and threading versions of each type of server can be created | |
|
34939
a1293ee6e1db
Fixed a typo in docstring I happened upon.
Guido van Rossum <guido@python.org>
parents:
34293
diff
changeset
|
53 using the ForkingMixIn and ThreadingMixIn mix-in classes. For |
| 2887 | 54 instance, a threading UDP server class is created as follows: |
| 55 | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
56 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
| 2887 | 57 |
|
5803
b56d527a433f
Fix bug found by kjpylint; change doc string to avoid "(" in column 1.
Guido van Rossum <guido@python.org>
parents:
4459
diff
changeset
|
58 The Mix-in class must come first, since it overrides a method defined |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
59 in UDPServer! Setting the various member variables also changes |
|
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
60 the behavior of the underlying server mechanism. |
| 2887 | 61 |
| 62 To implement a service, you must derive a class from | |
| 63 BaseRequestHandler and redefine its handle() method. You can then run | |
| 64 various versions of the service by combining one of the server classes | |
| 65 with your request handler class. | |
| 66 | |
| 67 The request handler class must be different for datagram or stream | |
|
35256
92b37c71f935
bug [ 957505 ] SocketServer module documentation misleading
Georg Brandl <georg@python.org>
parents:
34939
diff
changeset
|
68 services. This can be hidden by using the request handler |
|
92b37c71f935
bug [ 957505 ] SocketServer module documentation misleading
Georg Brandl <georg@python.org>
parents:
34939
diff
changeset
|
69 subclasses StreamRequestHandler or DatagramRequestHandler. |
| 2887 | 70 |
| 71 Of course, you still have to use your head! | |
| 72 | |
| 73 For instance, it makes no sense to use a forking server if the service | |
| 74 contains state in memory that can be modified by requests (since the | |
| 75 modifications in the child process would never reach the initial state | |
| 76 kept in the parent process and passed to each child). In this case, | |
| 77 you can use a threading server, but you will probably have to use | |
| 78 locks to avoid two requests that come in nearly simultaneous to apply | |
| 79 conflicting changes to the server state. | |
| 80 | |
| 81 On the other hand, if you are building e.g. an HTTP server, where all | |
| 82 data is stored externally (e.g. in the file system), a synchronous | |
| 83 class will essentially render the service "deaf" while one request is | |
| 84 being handled -- which may be for a very long time if a client is slow | |
| 85 to reqd all the data it has requested. Here a threading or forking | |
| 86 server is appropriate. | |
| 87 | |
| 88 In some cases, it may be appropriate to process part of a request | |
| 89 synchronously, but to finish processing in a forked child depending on | |
| 90 the request data. This can be implemented by using a synchronous | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
91 server and doing an explicit fork in the request handler class |
| 2887 | 92 handle() method. |
| 93 | |
| 94 Another approach to handling multiple simultaneous requests in an | |
| 95 environment that supports neither threads nor fork (or where these are | |
| 96 too expensive or inappropriate for the service) is to maintain an | |
| 97 explicit table of partially finished requests and to use select() to | |
| 98 decide which request to work on next (or whether to handle a new | |
| 99 incoming request). This is particularly important for stream services | |
| 100 where each client can potentially be connected for a long time (if | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
101 threads or subprocesses cannot be used). |
| 2887 | 102 |
| 103 Future work: | |
| 104 - Standard classes for Sun RPC (which uses either UDP or TCP) | |
| 105 - Standard mix-in classes to implement various authentication | |
| 106 and encryption schemes | |
| 107 - Standard framework for select-based multiplexing | |
| 108 | |
| 109 XXX Open problems: | |
| 110 - What to do with out-of-band data? | |
| 111 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
112 BaseServer: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
113 - split generic "request" functionality out into BaseServer class. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
114 Copyright (C) 2000 Luke Kenneth Casson Leighton <lkcl@samba.org> |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
115 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
116 example: read entries from a SQL database (requires overriding |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
117 get_request() to return a table entry from the database). |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
118 entry is processed by a RequestHandlerClass. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
119 |
| 2887 | 120 """ |
| 121 | |
|
16037
ba295e2c73ea
Bump __version__; add authorship note for the BaseServer patch.
Guido van Rossum <guido@python.org>
parents:
16010
diff
changeset
|
122 # Author of the BaseServer patch: Luke Kenneth Casson Leighton |
| 2887 | 123 |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
124 # XXX Warning! |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
125 # There is a test suite for this module, but it cannot be run by the |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
126 # standard regression test. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
127 # To run it manually, run Lib/test/test_socketserver.py. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
128 |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
129 __version__ = "0.4" |
| 2887 | 130 |
| 131 | |
| 132 import socket | |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
133 import select |
| 2887 | 134 import sys |
| 135 import os | |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
136 try: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
137 import threading |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
138 except ImportError: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
139 import dummy_threading as threading |
| 2887 | 140 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
141 __all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
142 "ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler", |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
143 "StreamRequestHandler","DatagramRequestHandler", |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
144 "ThreadingMixIn", "ForkingMixIn"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
145 if hasattr(socket, "AF_UNIX"): |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
146 __all__.extend(["UnixStreamServer","UnixDatagramServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
147 "ThreadingUnixStreamServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
148 "ThreadingUnixDatagramServer"]) |
| 2887 | 149 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
150 class BaseServer: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
151 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
152 """Base class for server classes. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
153 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
154 Methods for the caller: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
155 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
156 - __init__(server_address, RequestHandlerClass) |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
157 - serve_forever(poll_interval=0.5) |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
158 - shutdown() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
159 - handle_request() # if you do not use serve_forever() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
160 - fileno() -> int # for select() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
161 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
162 Methods that may be overridden: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
163 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
164 - server_bind() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
165 - server_activate() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
166 - get_request() -> request, client_address |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
167 - handle_timeout() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
168 - verify_request(request, client_address) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
169 - server_close() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
170 - process_request(request, client_address) |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
171 - close_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
172 - handle_error() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
173 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
174 Methods for derived classes: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
175 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
176 - finish_request(request, client_address) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
177 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
178 Class variables that may be overridden by derived classes or |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
179 instances: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
180 |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
181 - timeout |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
182 - address_family |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
183 - socket_type |
|
30664
298888e395ce
TCPServer: Fixed typo in class docstring.
Barry Warsaw <barry@python.org>
parents:
30663
diff
changeset
|
184 - allow_reuse_address |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
185 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
186 Instance variables: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
187 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
188 - RequestHandlerClass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
189 - socket |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
190 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
191 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
192 |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
193 timeout = None |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
194 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
195 def __init__(self, server_address, RequestHandlerClass): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
196 """Constructor. May be extended, do not override.""" |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
197 self.server_address = server_address |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
198 self.RequestHandlerClass = RequestHandlerClass |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
199 self.__is_shut_down = threading.Event() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
200 self.__serving = False |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
201 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
202 def server_activate(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
203 """Called by constructor to activate the server. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
204 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
205 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
206 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
207 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
208 pass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
209 |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
210 def serve_forever(self, poll_interval=0.5): |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
211 """Handle one request at a time until shutdown. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
212 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
213 Polls for shutdown every poll_interval seconds. Ignores |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
214 self.timeout. If you need to do periodic tasks, do them in |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
215 another thread. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
216 """ |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
217 self.__serving = True |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
218 self.__is_shut_down.clear() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
219 while self.__serving: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
220 # XXX: Consider using another file descriptor or |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
221 # connecting to the socket to wake this up instead of |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
222 # polling. Polling reduces our responsiveness to a |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
223 # shutdown request and wastes cpu at all other times. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
224 r, w, e = select.select([self], [], [], poll_interval) |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
225 if r: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
226 self._handle_request_noblock() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
227 self.__is_shut_down.set() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
228 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
229 def shutdown(self): |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
230 """Stops the serve_forever loop. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
231 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
232 Blocks until the loop has finished. This must be called while |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
233 serve_forever() is running in another thread, or it will |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
234 deadlock. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
235 """ |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
236 self.__serving = False |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
237 self.__is_shut_down.wait() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
238 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
239 # The distinction between handling, getting, processing and |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
240 # finishing a request is fairly arbitrary. Remember: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
241 # |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
242 # - handle_request() is the top-level call. It calls |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
243 # select, get_request(), verify_request() and process_request() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
244 # - get_request() is different for stream or datagram sockets |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
245 # - process_request() is the place that may fork a new process |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
246 # or create a new thread to finish the request |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
247 # - finish_request() instantiates the request handler class; |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
248 # this constructor will handle the request all by itself |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
249 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
250 def handle_request(self): |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
251 """Handle one request, possibly blocking. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
252 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
253 Respects self.timeout. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
254 """ |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
255 # Support people who used socket.settimeout() to escape |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
256 # handle_request before self.timeout was available. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
257 timeout = self.socket.gettimeout() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
258 if timeout is None: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
259 timeout = self.timeout |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
260 elif self.timeout is not None: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
261 timeout = min(timeout, self.timeout) |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
262 fd_sets = select.select([self], [], [], timeout) |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
263 if not fd_sets[0]: |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
264 self.handle_timeout() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
265 return |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
266 self._handle_request_noblock() |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
267 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
268 def _handle_request_noblock(self): |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
269 """Handle one request, without blocking. |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
270 |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
271 I assume that select.select has returned that the socket is |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
272 readable before this function was called, so there should be |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
273 no risk of blocking in get_request(). |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
274 """ |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
275 try: |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
276 request, client_address = self.get_request() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
277 except socket.error: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
278 return |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
279 if self.verify_request(request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
280 try: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
281 self.process_request(request, client_address) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
282 except: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
283 self.handle_error(request, client_address) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
284 self.close_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
285 |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
286 def handle_timeout(self): |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
287 """Called if no new request arrives within self.timeout. |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
288 |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
289 Overridden by ForkingMixIn. |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
290 """ |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
291 pass |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
292 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
293 def verify_request(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
294 """Verify the request. May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
295 |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20636
diff
changeset
|
296 Return True if we should proceed with this request. |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
297 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
298 """ |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20636
diff
changeset
|
299 return True |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
300 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
301 def process_request(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
302 """Call finish_request. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
303 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
304 Overridden by ForkingMixIn and ThreadingMixIn. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
305 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
306 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
307 self.finish_request(request, client_address) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
308 self.close_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
309 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
310 def server_close(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
311 """Called to clean-up the server. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
312 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
313 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
314 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
315 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
316 pass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
317 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
318 def finish_request(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
319 """Finish one request by instantiating RequestHandlerClass.""" |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
320 self.RequestHandlerClass(request, client_address, self) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
321 |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
322 def close_request(self, request): |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
323 """Called to clean up an individual request.""" |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
324 pass |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
325 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
326 def handle_error(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
327 """Handle an error gracefully. May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
328 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
329 The default is to print a traceback and continue. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
330 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
331 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
332 print '-'*40 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
333 print 'Exception happened during processing of request from', |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
334 print client_address |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
335 import traceback |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
336 traceback.print_exc() # XXX But this goes to stderr! |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
337 print '-'*40 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
338 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
339 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
340 class TCPServer(BaseServer): |
| 2887 | 341 |
| 342 """Base class for various socket-based server classes. | |
| 343 | |
| 344 Defaults to synchronous IP stream (i.e., TCP). | |
| 345 | |
| 346 Methods for the caller: | |
| 347 | |
|
41106
101a1ab573db
Patch #1599845: Add an option to disable the implicit calls to server_bind() and server_activate() in the constructors for TCPServer, SimpleXMLRPCServer and DocXMLRPCServer.
Collin Winter <collinw@gmail.com>
parents:
41098
diff
changeset
|
348 - __init__(server_address, RequestHandlerClass, bind_and_activate=True) |
|
45702
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
349 - serve_forever(poll_interval=0.5) |
|
a8754c0db53f
Progress on issue #1193577 by adding a polling .shutdown() method to
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45551
diff
changeset
|
350 - shutdown() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
351 - handle_request() # if you don't use serve_forever() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
352 - fileno() -> int # for select() |
| 2887 | 353 |
| 354 Methods that may be overridden: | |
| 355 | |
| 356 - server_bind() | |
| 357 - server_activate() | |
| 358 - get_request() -> request, client_address | |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
359 - handle_timeout() |
| 2887 | 360 - verify_request(request, client_address) |
| 361 - process_request(request, client_address) | |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
362 - close_request(request) |
| 2887 | 363 - handle_error() |
| 364 | |
| 365 Methods for derived classes: | |
| 366 | |
| 367 - finish_request(request, client_address) | |
| 368 | |
| 369 Class variables that may be overridden by derived classes or | |
| 370 instances: | |
| 371 | |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
372 - timeout |
| 2887 | 373 - address_family |
| 374 - socket_type | |
| 375 - request_queue_size (only for stream sockets) | |
|
30663
5c0174e2e14f
TCPServer: Fixed typo in class docstring.
Barry Warsaw <barry@python.org>
parents:
27010
diff
changeset
|
376 - allow_reuse_address |
| 2887 | 377 |
| 378 Instance variables: | |
| 379 | |
| 380 - server_address | |
| 381 - RequestHandlerClass | |
| 382 - socket | |
| 383 | |
| 384 """ | |
| 385 | |
| 386 address_family = socket.AF_INET | |
| 387 | |
| 388 socket_type = socket.SOCK_STREAM | |
| 389 | |
| 390 request_queue_size = 5 | |
| 391 | |
|
25023
a6de50c0d457
Replace 0 with False to match working in documentation. SF 599681.
Raymond Hettinger <python@rcn.com>
parents:
23035
diff
changeset
|
392 allow_reuse_address = False |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
393 |
|
41106
101a1ab573db
Patch #1599845: Add an option to disable the implicit calls to server_bind() and server_activate() in the constructors for TCPServer, SimpleXMLRPCServer and DocXMLRPCServer.
Collin Winter <collinw@gmail.com>
parents:
41098
diff
changeset
|
394 def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
395 """Constructor. May be extended, do not override.""" |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
396 BaseServer.__init__(self, server_address, RequestHandlerClass) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
397 self.socket = socket.socket(self.address_family, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
398 self.socket_type) |
|
41106
101a1ab573db
Patch #1599845: Add an option to disable the implicit calls to server_bind() and server_activate() in the constructors for TCPServer, SimpleXMLRPCServer and DocXMLRPCServer.
Collin Winter <collinw@gmail.com>
parents:
41098
diff
changeset
|
399 if bind_and_activate: |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41106
diff
changeset
|
400 self.server_bind() |
|
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41106
diff
changeset
|
401 self.server_activate() |
| 2887 | 402 |
| 403 def server_bind(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
404 """Called by constructor to bind the socket. |
| 2887 | 405 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
406 May be overridden. |
| 2887 | 407 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
408 """ |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
409 if self.allow_reuse_address: |
|
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
410 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
411 self.socket.bind(self.server_address) |
|
41098
1bbd688a6de1
Bug #1531963: Make SocketServer.TCPServer's server_address always be equal to calling getsockname() on the server's socket.
Collin Winter <collinw@gmail.com>
parents:
35256
diff
changeset
|
412 self.server_address = self.socket.getsockname() |
| 2887 | 413 |
| 414 def server_activate(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
415 """Called by constructor to activate the server. |
| 2887 | 416 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
417 May be overridden. |
| 2887 | 418 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
419 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
420 self.socket.listen(self.request_queue_size) |
| 2887 | 421 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
422 def server_close(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
423 """Called to clean-up the server. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
424 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
425 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
426 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
427 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
428 self.socket.close() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
429 |
| 2887 | 430 def fileno(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
431 """Return socket file number. |
| 2887 | 432 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
433 Interface required by select(). |
| 2887 | 434 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
435 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
436 return self.socket.fileno() |
| 2887 | 437 |
| 438 def get_request(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
439 """Get the request and client address from the socket. |
| 2887 | 440 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
441 May be overridden. |
| 2887 | 442 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
443 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
444 return self.socket.accept() |
| 2887 | 445 |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
446 def close_request(self, request): |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
447 """Called to clean up an individual request.""" |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
448 request.close() |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
449 |
| 2887 | 450 |
| 451 class UDPServer(TCPServer): | |
| 452 | |
| 453 """UDP server class.""" | |
| 454 | |
|
25023
a6de50c0d457
Replace 0 with False to match working in documentation. SF 599681.
Raymond Hettinger <python@rcn.com>
parents:
23035
diff
changeset
|
455 allow_reuse_address = False |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
456 |
| 2887 | 457 socket_type = socket.SOCK_DGRAM |
| 458 | |
| 459 max_packet_size = 8192 | |
| 460 | |
| 461 def get_request(self): | |
|
8510
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
462 data, client_addr = self.socket.recvfrom(self.max_packet_size) |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
463 return (data, self.socket), client_addr |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
464 |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
465 def server_activate(self): |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
466 # No need to call listen() for UDP. |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
467 pass |
| 2887 | 468 |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
469 def close_request(self, request): |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
470 # No need to close anything. |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
471 pass |
| 2887 | 472 |
| 473 class ForkingMixIn: | |
| 474 | |
| 475 """Mix-in class to handle each request in a new process.""" | |
| 476 | |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
477 timeout = 300 |
| 2887 | 478 active_children = None |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
479 max_children = 40 |
| 2887 | 480 |
| 481 def collect_children(self): | |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
482 """Internal routine to wait for children that have exited.""" |
|
45551
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
483 if self.active_children is None: return |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
484 while len(self.active_children) >= self.max_children: |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
485 # XXX: This will wait for any child process, not just ones |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
486 # spawned by this library. This could confuse other |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
487 # libraries that expect to be able to wait for their own |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
488 # children. |
|
11001
926cb5a5ee30
In collect_children(), put a try-except around os.waitpid() because it
Guido van Rossum <guido@python.org>
parents:
10992
diff
changeset
|
489 try: |
|
45551
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
490 pid, status = os.waitpid(0, options=0) |
|
11001
926cb5a5ee30
In collect_children(), put a try-except around os.waitpid() because it
Guido van Rossum <guido@python.org>
parents:
10992
diff
changeset
|
491 except os.error: |
|
926cb5a5ee30
In collect_children(), put a try-except around os.waitpid() because it
Guido van Rossum <guido@python.org>
parents:
10992
diff
changeset
|
492 pid = None |
|
45551
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
493 if pid not in self.active_children: continue |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
494 self.active_children.remove(pid) |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
495 |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
496 # XXX: This loop runs more system calls than it ought |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
497 # to. There should be a way to put the active_children into a |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
498 # process group and then use os.waitpid(-pgid) to wait for any |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
499 # of that set, but I couldn't find a way to allocate pgids |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
500 # that couldn't collide. |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
501 for child in self.active_children: |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
502 try: |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
503 pid, status = os.waitpid(child, os.WNOHANG) |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
504 except os.error: |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
505 pid = None |
|
cc220745e4e5
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45334
diff
changeset
|
506 if not pid: continue |
|
45334
e518e39f4a9e
Add diagnostic message to help figure-out why SocketServer tests occasionally crash
Raymond Hettinger <python@rcn.com>
parents:
44768
diff
changeset
|
507 try: |
|
e518e39f4a9e
Add diagnostic message to help figure-out why SocketServer tests occasionally crash
Raymond Hettinger <python@rcn.com>
parents:
44768
diff
changeset
|
508 self.active_children.remove(pid) |
|
e518e39f4a9e
Add diagnostic message to help figure-out why SocketServer tests occasionally crash
Raymond Hettinger <python@rcn.com>
parents:
44768
diff
changeset
|
509 except ValueError, e: |
|
e518e39f4a9e
Add diagnostic message to help figure-out why SocketServer tests occasionally crash
Raymond Hettinger <python@rcn.com>
parents:
44768
diff
changeset
|
510 raise ValueError('%s. x=%d and list=%r' % (e.message, pid, |
|
e518e39f4a9e
Add diagnostic message to help figure-out why SocketServer tests occasionally crash
Raymond Hettinger <python@rcn.com>
parents:
44768
diff
changeset
|
511 self.active_children)) |
| 2887 | 512 |
|
44768
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
513 def handle_timeout(self): |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
514 """Wait for zombies after self.timeout seconds of inactivity. |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
515 |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
516 May be extended, do not override. |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
517 """ |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
518 self.collect_children() |
|
cde41bd08e5e
Patch #742598 from Michael Pomraning: add .timeout attribute to SocketServer that will call
Andrew M. Kuchling <amk@amk.ca>
parents:
41133
diff
changeset
|
519 |
| 2887 | 520 def process_request(self, request, client_address): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
521 """Fork a new subprocess to process the request.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
522 self.collect_children() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
523 pid = os.fork() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
524 if pid: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
525 # Parent process |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
526 if self.active_children is None: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
527 self.active_children = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
528 self.active_children.append(pid) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
529 self.close_request(request) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
530 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
531 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
532 # Child process. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
533 # This must never return, hence os._exit()! |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
534 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
535 self.finish_request(request, client_address) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
536 os._exit(0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
537 except: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
538 try: |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
539 self.handle_error(request, client_address) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
540 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
541 os._exit(1) |
| 2887 | 542 |
| 543 | |
| 544 class ThreadingMixIn: | |
| 545 """Mix-in class to handle each request in a new thread.""" | |
| 546 | |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
547 # Decides how threads will act upon termination of the |
|
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
548 # main process |
| 26347 | 549 daemon_threads = False |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
550 |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
551 def process_request_thread(self, request, client_address): |
|
20636
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
552 """Same as in BaseServer but as a thread. |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
553 |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
554 In addition, exception handling is done here. |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
555 |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
556 """ |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
557 try: |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
558 self.finish_request(request, client_address) |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
559 self.close_request(request) |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
560 except: |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
561 self.handle_error(request, client_address) |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
562 self.close_request(request) |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
563 |
| 2887 | 564 def process_request(self, request, client_address): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
565 """Start a new thread to process the request.""" |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
566 t = threading.Thread(target = self.process_request_thread, |
|
11382
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
567 args = (request, client_address)) |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
568 if self.daemon_threads: |
|
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
569 t.setDaemon (1) |
|
11382
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
570 t.start() |
| 2887 | 571 |
| 572 | |
| 573 class ForkingUDPServer(ForkingMixIn, UDPServer): pass | |
| 574 class ForkingTCPServer(ForkingMixIn, TCPServer): pass | |
| 575 | |
| 576 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass | |
| 577 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass | |
| 578 | |
|
9544
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
579 if hasattr(socket, 'AF_UNIX'): |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
580 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
581 class UnixStreamServer(TCPServer): |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
582 address_family = socket.AF_UNIX |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
583 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
584 class UnixDatagramServer(UDPServer): |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
585 address_family = socket.AF_UNIX |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
586 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
587 class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer): pass |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
588 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
589 class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass |
| 2887 | 590 |
| 591 class BaseRequestHandler: | |
| 592 | |
| 593 """Base class for request handler classes. | |
| 594 | |
| 595 This class is instantiated for each request to be handled. The | |
| 596 constructor sets the instance variables request, client_address | |
| 597 and server, and then calls the handle() method. To implement a | |
| 598 specific service, all you need to do is to derive a class which | |
| 599 defines a handle() method. | |
| 600 | |
| 601 The handle() method can find the request as self.request, the | |
|
9485
6e6be896ec46
Fix typo in docstring: client_request should be client_address.
Guido van Rossum <guido@python.org>
parents:
8510
diff
changeset
|
602 client address as self.client_address, and the server (in case it |
| 2887 | 603 needs access to per-server information) as self.server. Since a |
| 604 separate instance is created for each request, the handle() method | |
| 605 can define arbitrary other instance variariables. | |
| 606 | |
| 607 """ | |
| 608 | |
| 609 def __init__(self, request, client_address, server): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
610 self.request = request |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
611 self.client_address = client_address |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
612 self.server = server |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
613 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
614 self.setup() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
615 self.handle() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
616 self.finish() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
617 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
618 sys.exc_traceback = None # Help garbage collection |
| 2887 | 619 |
| 620 def setup(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
621 pass |
| 2887 | 622 |
| 623 def handle(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
624 pass |
| 2887 | 625 |
| 626 def finish(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
627 pass |
| 2887 | 628 |
| 629 | |
| 630 # The following two classes make it possible to use the same service | |
| 631 # class for stream or datagram servers. | |
| 632 # Each class sets up these instance variables: | |
| 633 # - rfile: a file object from which receives the request is read | |
| 634 # - wfile: a file object to which the reply is written | |
| 635 # When the handle() method returns, wfile is flushed properly | |
| 636 | |
| 637 | |
| 638 class StreamRequestHandler(BaseRequestHandler): | |
| 639 | |
| 640 """Define self.rfile and self.wfile for stream sockets.""" | |
| 641 | |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
642 # Default buffer sizes for rfile, wfile. |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
643 # We default rfile to buffered because otherwise it could be |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
644 # really slow for large data (a getc() call per byte); we make |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
645 # wfile unbuffered because (a) often after a write() we want to |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
646 # read and we need to flush the line; (b) big writes to unbuffered |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
647 # files are typically optimized by stdio even when big reads |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
648 # aren't. |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
649 rbufsize = -1 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
650 wbufsize = 0 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
651 |
| 2887 | 652 def setup(self): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
653 self.connection = self.request |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
654 self.rfile = self.connection.makefile('rb', self.rbufsize) |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
655 self.wfile = self.connection.makefile('wb', self.wbufsize) |
| 2887 | 656 |
| 657 def finish(self): | |
|
27010
2687a9085411
Clearing out old patch queue. Patch #558547, make SocketServer more
Anthony Baxter <anthonybaxter@gmail.com>
parents:
26347
diff
changeset
|
658 if not self.wfile.closed: |
|
2687a9085411
Clearing out old patch queue. Patch #558547, make SocketServer more
Anthony Baxter <anthonybaxter@gmail.com>
parents:
26347
diff
changeset
|
659 self.wfile.flush() |
|
7840
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
660 self.wfile.close() |
|
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
661 self.rfile.close() |
| 2887 | 662 |
| 663 | |
| 664 class DatagramRequestHandler(BaseRequestHandler): | |
| 665 | |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
666 # XXX Regrettably, I cannot get this working on Linux; |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
667 # s.recvfrom() doesn't return a meaningful client address. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
668 |
| 2887 | 669 """Define self.rfile and self.wfile for datagram sockets.""" |
| 670 | |
| 671 def setup(self): | |
|
34293
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
30664
diff
changeset
|
672 try: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
30664
diff
changeset
|
673 from cStringIO import StringIO |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
30664
diff
changeset
|
674 except ImportError: |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
30664
diff
changeset
|
675 from StringIO import StringIO |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
676 self.packet, self.socket = self.request |
|
34293
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
30664
diff
changeset
|
677 self.rfile = StringIO(self.packet) |
|
cd517701fd93
Use cStringIO where available.
Raymond Hettinger <python@rcn.com>
parents:
30664
diff
changeset
|
678 self.wfile = StringIO() |
| 2887 | 679 |
| 680 def finish(self): | |
|
8510
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
681 self.socket.sendto(self.wfile.getvalue(), self.client_address) |
