Mercurial > cpython-lru_cache-weakref
annotate Lib/SocketServer.py @ 15164:8930ec3bc00a legacy-trunk
Added additional test cases for pulldom modifications.
| author | Lars Gustäbel <lars@gustaebel.de> |
|---|---|
| date | Wed, 11 Oct 2000 22:36:00 +0000 |
| parents | cbe423b3ba3a |
| children | d3f6f3155c24 |
| rev | line source |
|---|---|
| 2887 | 1 """Generic socket server classes. |
| 2 | |
| 3 This module tries to capture the various aspects of defining a server: | |
| 4 | |
| 5 - address family: | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
6 - AF_INET: IP (Internet Protocol) sockets (default) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
7 - 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
|
8 - others, e.g. AF_DECNET are conceivable (see <socket.h> |
| 2887 | 9 - socket type: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
10 - 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
|
11 - SOCK_DGRAM (datagrams, e.g. UDP) |
| 2887 | 12 - 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
|
13 (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
|
14 at the request before anything else, e.g. logging) |
| 2887 | 15 - 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
|
16 - 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
|
17 - 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
|
18 - threading (each request is handled by a new thread) |
| 2887 | 19 |
| 20 The classes in this module favor the server type that is simplest to | |
| 21 write: a synchronous TCP/IP server. This is bad class design, but | |
| 22 save some typing. (There's also the issue that a deep class hierarchy | |
| 23 slows down method lookups.) | |
| 24 | |
| 25 There are four classes in an inheritance diagram that represent | |
| 26 synchronous servers of four types: | |
| 27 | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
28 +-----------+ +------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
29 | TCPServer |------->| UnixStreamServer | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
30 +-----------+ +------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
31 | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
32 v |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
33 +-----------+ +--------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
34 | UDPServer |------->| UnixDatagramServer | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
35 +-----------+ +--------------------+ |
| 2887 | 36 |
|
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
|
37 Note that UnixDatagramServer derives from UDPServer, not from |
| 2887 | 38 UnixStreamServer -- the only difference between an IP and a Unix |
| 39 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
|
40 unix server classes. |
| 2887 | 41 |
| 42 Forking and threading versions of each type of server can be created | |
| 43 using the ForkingServer and ThreadingServer mix-in classes. For | |
| 44 instance, a threading UDP server class is created as follows: | |
| 45 | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
46 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
| 2887 | 47 |
|
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
|
48 The Mix-in class must come first, since it overrides a method defined |
|
b56d527a433f
Fix bug found by kjpylint; change doc string to avoid "(" in column 1.
Guido van Rossum <guido@python.org>
parents:
4459
diff
changeset
|
49 in UDPServer! |
| 2887 | 50 |
| 51 To implement a service, you must derive a class from | |
| 52 BaseRequestHandler and redefine its handle() method. You can then run | |
| 53 various versions of the service by combining one of the server classes | |
| 54 with your request handler class. | |
| 55 | |
| 56 The request handler class must be different for datagram or stream | |
| 57 services. This can be hidden by using the mix-in request handler | |
| 58 classes StreamRequestHandler or DatagramRequestHandler. | |
| 59 | |
| 60 Of course, you still have to use your head! | |
| 61 | |
| 62 For instance, it makes no sense to use a forking server if the service | |
| 63 contains state in memory that can be modified by requests (since the | |
| 64 modifications in the child process would never reach the initial state | |
| 65 kept in the parent process and passed to each child). In this case, | |
| 66 you can use a threading server, but you will probably have to use | |
| 67 locks to avoid two requests that come in nearly simultaneous to apply | |
| 68 conflicting changes to the server state. | |
| 69 | |
| 70 On the other hand, if you are building e.g. an HTTP server, where all | |
| 71 data is stored externally (e.g. in the file system), a synchronous | |
| 72 class will essentially render the service "deaf" while one request is | |
| 73 being handled -- which may be for a very long time if a client is slow | |
| 74 to reqd all the data it has requested. Here a threading or forking | |
| 75 server is appropriate. | |
| 76 | |
| 77 In some cases, it may be appropriate to process part of a request | |
| 78 synchronously, but to finish processing in a forked child depending on | |
| 79 the request data. This can be implemented by using a synchronous | |
| 80 server and doing an explicit fork in the request handler class's | |
| 81 handle() method. | |
| 82 | |
| 83 Another approach to handling multiple simultaneous requests in an | |
| 84 environment that supports neither threads nor fork (or where these are | |
| 85 too expensive or inappropriate for the service) is to maintain an | |
| 86 explicit table of partially finished requests and to use select() to | |
| 87 decide which request to work on next (or whether to handle a new | |
| 88 incoming request). This is particularly important for stream services | |
| 89 where each client can potentially be connected for a long time (if | |
| 90 threads or subprocesses can't be used). | |
| 91 | |
| 92 Future work: | |
| 93 - Standard classes for Sun RPC (which uses either UDP or TCP) | |
| 94 - Standard mix-in classes to implement various authentication | |
| 95 and encryption schemes | |
| 96 - Standard framework for select-based multiplexing | |
| 97 | |
| 98 XXX Open problems: | |
| 99 - What to do with out-of-band data? | |
| 100 | |
| 101 """ | |
| 102 | |
| 103 | |
| 104 __version__ = "0.2" | |
| 105 | |
| 106 | |
| 107 import socket | |
| 108 import sys | |
| 109 import os | |
| 110 | |
| 111 | |
| 112 class TCPServer: | |
| 113 | |
| 114 """Base class for various socket-based server classes. | |
| 115 | |
| 116 Defaults to synchronous IP stream (i.e., TCP). | |
| 117 | |
| 118 Methods for the caller: | |
| 119 | |
| 120 - __init__(server_address, RequestHandlerClass) | |
| 121 - serve_forever() | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
122 - 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
|
123 - fileno() -> int # for select() |
| 2887 | 124 |
| 125 Methods that may be overridden: | |
| 126 | |
| 127 - server_bind() | |
| 128 - server_activate() | |
| 129 - get_request() -> request, client_address | |
| 130 - verify_request(request, client_address) | |
| 131 - process_request(request, client_address) | |
| 132 - handle_error() | |
| 133 | |
| 134 Methods for derived classes: | |
| 135 | |
| 136 - finish_request(request, client_address) | |
| 137 | |
| 138 Class variables that may be overridden by derived classes or | |
| 139 instances: | |
| 140 | |
| 141 - address_family | |
| 142 - socket_type | |
| 143 - request_queue_size (only for stream sockets) | |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
144 - reuse_address |
| 2887 | 145 |
| 146 Instance variables: | |
| 147 | |
| 148 - server_address | |
| 149 - RequestHandlerClass | |
| 150 - socket | |
| 151 | |
| 152 """ | |
| 153 | |
| 154 address_family = socket.AF_INET | |
| 155 | |
| 156 socket_type = socket.SOCK_STREAM | |
| 157 | |
| 158 request_queue_size = 5 | |
| 159 | |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
160 allow_reuse_address = 0 |
|
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
161 |
| 2887 | 162 def __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
|
163 """Constructor. May be extended, do not override.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
164 self.server_address = server_address |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
165 self.RequestHandlerClass = RequestHandlerClass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
166 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
|
167 self.socket_type) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
168 self.server_bind() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
169 self.server_activate() |
| 2887 | 170 |
| 171 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
|
172 """Called by constructor to bind the socket. |
| 2887 | 173 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
174 May be overridden. |
| 2887 | 175 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
176 """ |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
177 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
|
178 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
|
179 self.socket.bind(self.server_address) |
| 2887 | 180 |
| 181 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
|
182 """Called by constructor to activate the server. |
| 2887 | 183 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
184 May be overridden. |
| 2887 | 185 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
186 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
187 self.socket.listen(self.request_queue_size) |
| 2887 | 188 |
| 189 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
|
190 """Return socket file number. |
| 2887 | 191 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
192 Interface required by select(). |
| 2887 | 193 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
194 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
195 return self.socket.fileno() |
| 2887 | 196 |
| 197 def serve_forever(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
198 """Handle one request at a time until doomsday.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
199 while 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
200 self.handle_request() |
| 2887 | 201 |
| 202 # The distinction between handling, getting, processing and | |
| 203 # finishing a request is fairly arbitrary. Remember: | |
| 204 # | |
| 205 # - handle_request() is the top-level call. It calls | |
| 206 # get_request(), verify_request() and process_request() | |
| 207 # - get_request() is different for stream or datagram sockets | |
| 208 # - process_request() is the place that may fork a new process | |
| 209 # or create a new thread to finish the request | |
| 210 # - finish_request() instantiates the request handler class; | |
| 211 # this constructor will handle the request all by itself | |
| 212 | |
| 213 def handle_request(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
214 """Handle one request, possibly blocking.""" |
|
10992
87d3f91e44d4
Laurence Tratt notes that the accept() call in get_request() can fail,
Guido van Rossum <guido@python.org>
parents:
10902
diff
changeset
|
215 try: |
|
87d3f91e44d4
Laurence Tratt notes that the accept() call in get_request() can fail,
Guido van Rossum <guido@python.org>
parents:
10902
diff
changeset
|
216 request, client_address = self.get_request() |
|
87d3f91e44d4
Laurence Tratt notes that the accept() call in get_request() can fail,
Guido van Rossum <guido@python.org>
parents:
10902
diff
changeset
|
217 except socket.error: |
|
87d3f91e44d4
Laurence Tratt notes that the accept() call in get_request() can fail,
Guido van Rossum <guido@python.org>
parents:
10902
diff
changeset
|
218 return |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
219 if self.verify_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
|
220 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
221 self.process_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
|
222 except: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
223 self.handle_error(request, client_address) |
| 2887 | 224 |
| 225 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
|
226 """Get the request and client address from the socket. |
| 2887 | 227 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
228 May be overridden. |
| 2887 | 229 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
230 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
231 return self.socket.accept() |
| 2887 | 232 |
| 233 def verify_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
|
234 """Verify the request. May be overridden. |
| 2887 | 235 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
236 Return true if we should proceed with this request. |
| 2887 | 237 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
238 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
239 return 1 |
| 2887 | 240 |
| 241 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
|
242 """Call finish_request. |
| 2887 | 243 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
244 Overridden by ForkingMixIn and ThreadingMixIn. |
| 2887 | 245 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
246 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
247 self.finish_request(request, client_address) |
| 2887 | 248 |
| 249 def finish_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
|
250 """Finish one request by instantiating RequestHandlerClass.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
251 self.RequestHandlerClass(request, client_address, self) |
| 2887 | 252 |
| 253 def handle_error(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
|
254 """Handle an error gracefully. May be overridden. |
| 2887 | 255 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
256 The default is to print a traceback and continue. |
| 2887 | 257 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
258 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
259 print '-'*40 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
260 print 'Exception happened during processing of request from', |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
261 print client_address |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
262 import traceback |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
263 traceback.print_exc() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
264 print '-'*40 |
| 2887 | 265 |
| 266 | |
| 267 class UDPServer(TCPServer): | |
| 268 | |
| 269 """UDP server class.""" | |
| 270 | |
| 271 socket_type = socket.SOCK_DGRAM | |
| 272 | |
| 273 max_packet_size = 8192 | |
| 274 | |
| 275 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
|
276 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
|
277 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
|
278 |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
279 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
|
280 # 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
|
281 pass |
| 2887 | 282 |
| 283 | |
| 284 class ForkingMixIn: | |
| 285 | |
| 286 """Mix-in class to handle each request in a new process.""" | |
| 287 | |
| 288 active_children = None | |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
289 max_children = 40 |
| 2887 | 290 |
| 291 def collect_children(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
292 """Internal routine to wait for died children.""" |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
293 while self.active_children: |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
294 if len(self.active_children) < self.max_children: |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
295 options = os.WNOHANG |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
296 else: |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
297 # If the maximum number of children are already |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
298 # running, block while waiting for a child to exit |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
299 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
|
300 try: |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
301 pid, status = os.waitpid(0, options) |
|
11001
926cb5a5ee30
In collect_children(), put a try-except around os.waitpid() because it
Guido van Rossum <guido@python.org>
parents:
10992
diff
changeset
|
302 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
|
303 pid = None |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
304 if not pid: break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
305 self.active_children.remove(pid) |
| 2887 | 306 |
| 307 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
|
308 """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
|
309 self.collect_children() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
310 pid = os.fork() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
311 if pid: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
312 # Parent process |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
313 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
|
314 self.active_children = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
315 self.active_children.append(pid) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
316 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
317 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
318 # Child process. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
319 # 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
|
320 try: |
|
10902
7e56d39d0741
Andy Dustman notes that I patched the close() call in the wrong place.
Guido van Rossum <guido@python.org>
parents:
10872
diff
changeset
|
321 self.socket.close() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
322 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
|
323 os._exit(0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
324 except: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
325 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
326 self.handle_error(request, |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
327 client_address) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
328 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
329 os._exit(1) |
| 2887 | 330 |
| 331 | |
| 332 class ThreadingMixIn: | |
| 333 """Mix-in class to handle each request in a new thread.""" | |
| 334 | |
| 335 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
|
336 """Start a new thread to process the request.""" |
|
11382
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
337 import threading |
|
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
338 t = threading.Thread(target = self.finish_request, |
|
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
339 args = (request, client_address)) |
|
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
340 t.start() |
| 2887 | 341 |
| 342 | |
| 343 class ForkingUDPServer(ForkingMixIn, UDPServer): pass | |
| 344 class ForkingTCPServer(ForkingMixIn, TCPServer): pass | |
| 345 | |
| 346 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass | |
| 347 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass | |
| 348 | |
|
9544
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
349 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
|
350 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
351 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
|
352 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
|
353 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
354 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
|
355 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
|
356 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
357 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
|
358 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
359 class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass |
| 2887 | 360 |
| 361 class BaseRequestHandler: | |
| 362 | |
| 363 """Base class for request handler classes. | |
| 364 | |
| 365 This class is instantiated for each request to be handled. The | |
| 366 constructor sets the instance variables request, client_address | |
| 367 and server, and then calls the handle() method. To implement a | |
| 368 specific service, all you need to do is to derive a class which | |
| 369 defines a handle() method. | |
| 370 | |
| 371 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
|
372 client address as self.client_address, and the server (in case it |
| 2887 | 373 needs access to per-server information) as self.server. Since a |
| 374 separate instance is created for each request, the handle() method | |
| 375 can define arbitrary other instance variariables. | |
| 376 | |
| 377 """ | |
| 378 | |
| 379 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
|
380 self.request = request |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
381 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
|
382 self.server = server |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
383 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
384 self.setup() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
385 self.handle() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
386 self.finish() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
387 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
388 sys.exc_traceback = None # Help garbage collection |
| 2887 | 389 |
| 390 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
|
391 pass |
| 2887 | 392 |
| 393 def __del__(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
394 pass |
| 2887 | 395 |
| 396 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
|
397 pass |
| 2887 | 398 |
| 399 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
|
400 pass |
| 2887 | 401 |
| 402 | |
| 403 # The following two classes make it possible to use the same service | |
| 404 # class for stream or datagram servers. | |
| 405 # Each class sets up these instance variables: | |
| 406 # - rfile: a file object from which receives the request is read | |
| 407 # - wfile: a file object to which the reply is written | |
| 408 # When the handle() method returns, wfile is flushed properly | |
| 409 | |
| 410 | |
| 411 class StreamRequestHandler(BaseRequestHandler): | |
| 412 | |
| 413 """Define self.rfile and self.wfile for stream sockets.""" | |
| 414 | |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
415 # 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
|
416 # 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
|
417 # 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
|
418 # 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
|
419 # 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
|
420 # 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
|
421 # aren't. |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
422 rbufsize = -1 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
423 wbufsize = 0 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
424 |
| 2887 | 425 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
|
426 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
|
427 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
|
428 self.wfile = self.connection.makefile('wb', self.wbufsize) |
| 2887 | 429 |
| 430 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
|
431 self.wfile.flush() |
|
7840
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
432 self.wfile.close() |
|
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
433 self.rfile.close() |
| 2887 | 434 |
| 435 | |
| 436 class DatagramRequestHandler(BaseRequestHandler): | |
| 437 | |
| 438 """Define self.rfile and self.wfile for datagram sockets.""" | |
| 439 | |
| 440 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
|
441 import StringIO |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
442 self.packet, self.socket = self.request |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
443 self.rfile = StringIO.StringIO(self.packet) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
444 self.wfile = StringIO.StringIO(self.packet) |
| 2887 | 445 |
| 446 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
|
447 self.socket.sendto(self.wfile.getvalue(), self.client_address) |
