Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 1 | """Generic socket server classes. |
| 2 | |
| 3 | This module tries to capture the various aspects of defining a server: |
| 4 | |
| 5 | - address family: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 6 | - AF_INET: IP (Internet Protocol) sockets (default) |
| 7 | - AF_UNIX: Unix domain sockets |
| 8 | - others, e.g. AF_DECNET are conceivable (see <socket.h> |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 9 | - socket type: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 10 | - SOCK_STREAM (reliable stream, e.g. TCP) |
| 11 | - SOCK_DGRAM (datagrams, e.g. UDP) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 12 | - client address verification before further looking at the request |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 13 | (This is actually a hook for any processing that needs to look |
| 14 | at the request before anything else, e.g. logging) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 15 | - how to handle multiple requests: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 16 | - synchronous (one request is handled at a time) |
| 17 | - forking (each request is handled by a new process) |
| 18 | - threading (each request is handled by a new thread) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 28 | +-----------+ +------------------+ |
| 29 | | TCPServer |------->| UnixStreamServer | |
| 30 | +-----------+ +------------------+ |
| 31 | | |
| 32 | v |
| 33 | +-----------+ +--------------------+ |
| 34 | | UDPServer |------->| UnixDatagramServer | |
| 35 | +-----------+ +--------------------+ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | db2b70c | 1997-07-16 16:21:38 +0000 | [diff] [blame] | 37 | Note that UnixDatagramServer derives from UDPServer, not from |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 38 | UnixStreamServer -- the only difference between an IP and a Unix |
| 39 | stream server is the address family, which is simply repeated in both |
Guido van Rossum | db2b70c | 1997-07-16 16:21:38 +0000 | [diff] [blame] | 40 | unix server classes. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 46 | class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | db2b70c | 1997-07-16 16:21:38 +0000 | [diff] [blame] | 48 | The Mix-in class must come first, since it overrides a method defined |
| 49 | in UDPServer! |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 122 | - handle_request() # if you don't use serve_forever() |
| 123 | - fileno() -> int # for select() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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) |
| 144 | |
| 145 | Instance variables: |
| 146 | |
| 147 | - server_address |
| 148 | - RequestHandlerClass |
| 149 | - socket |
| 150 | |
| 151 | """ |
| 152 | |
| 153 | address_family = socket.AF_INET |
| 154 | |
| 155 | socket_type = socket.SOCK_STREAM |
| 156 | |
| 157 | request_queue_size = 5 |
| 158 | |
| 159 | def __init__(self, server_address, RequestHandlerClass): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 160 | """Constructor. May be extended, do not override.""" |
| 161 | self.server_address = server_address |
| 162 | self.RequestHandlerClass = RequestHandlerClass |
| 163 | self.socket = socket.socket(self.address_family, |
| 164 | self.socket_type) |
| 165 | self.server_bind() |
| 166 | self.server_activate() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 167 | |
| 168 | def server_bind(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | """Called by constructor to bind the socket. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 171 | May be overridden. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 173 | """ |
| 174 | self.socket.bind(self.server_address) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 175 | |
| 176 | def server_activate(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 177 | """Called by constructor to activate the server. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 179 | May be overridden. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 181 | """ |
| 182 | self.socket.listen(self.request_queue_size) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 183 | |
| 184 | def fileno(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 185 | """Return socket file number. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 187 | Interface required by select(). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | """ |
| 190 | return self.socket.fileno() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 191 | |
| 192 | def serve_forever(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 193 | """Handle one request at a time until doomsday.""" |
| 194 | while 1: |
| 195 | self.handle_request() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 196 | |
| 197 | # The distinction between handling, getting, processing and |
| 198 | # finishing a request is fairly arbitrary. Remember: |
| 199 | # |
| 200 | # - handle_request() is the top-level call. It calls |
| 201 | # get_request(), verify_request() and process_request() |
| 202 | # - get_request() is different for stream or datagram sockets |
| 203 | # - process_request() is the place that may fork a new process |
| 204 | # or create a new thread to finish the request |
| 205 | # - finish_request() instantiates the request handler class; |
| 206 | # this constructor will handle the request all by itself |
| 207 | |
| 208 | def handle_request(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 209 | """Handle one request, possibly blocking.""" |
Guido van Rossum | f2f0594 | 1999-06-15 22:25:32 +0000 | [diff] [blame] | 210 | try: |
| 211 | request, client_address = self.get_request() |
| 212 | except socket.error: |
| 213 | return |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 214 | if self.verify_request(request, client_address): |
| 215 | try: |
| 216 | self.process_request(request, client_address) |
| 217 | except: |
| 218 | self.handle_error(request, client_address) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 219 | |
| 220 | def get_request(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 221 | """Get the request and client address from the socket. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 223 | May be overridden. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 225 | """ |
| 226 | return self.socket.accept() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 227 | |
| 228 | def verify_request(self, request, client_address): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 229 | """Verify the request. May be overridden. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 231 | Return true if we should proceed with this request. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 233 | """ |
| 234 | return 1 |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 235 | |
| 236 | def process_request(self, request, client_address): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 237 | """Call finish_request. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 239 | Overridden by ForkingMixIn and ThreadingMixIn. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 241 | """ |
| 242 | self.finish_request(request, client_address) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 243 | |
| 244 | def finish_request(self, request, client_address): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 245 | """Finish one request by instantiating RequestHandlerClass.""" |
| 246 | self.RequestHandlerClass(request, client_address, self) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 247 | |
| 248 | def handle_error(self, request, client_address): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 249 | """Handle an error gracefully. May be overridden. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 251 | The default is to print a traceback and continue. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 253 | """ |
| 254 | print '-'*40 |
| 255 | print 'Exception happened during processing of request from', |
| 256 | print client_address |
| 257 | import traceback |
| 258 | traceback.print_exc() |
| 259 | print '-'*40 |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 260 | |
| 261 | |
| 262 | class UDPServer(TCPServer): |
| 263 | |
| 264 | """UDP server class.""" |
| 265 | |
| 266 | socket_type = socket.SOCK_DGRAM |
| 267 | |
| 268 | max_packet_size = 8192 |
| 269 | |
| 270 | def get_request(self): |
Guido van Rossum | 3249082 | 1998-06-16 02:27:33 +0000 | [diff] [blame] | 271 | data, client_addr = self.socket.recvfrom(self.max_packet_size) |
| 272 | return (data, self.socket), client_addr |
| 273 | |
| 274 | def server_activate(self): |
| 275 | # No need to call listen() for UDP. |
| 276 | pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 277 | |
| 278 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 279 | class ForkingMixIn: |
| 280 | |
| 281 | """Mix-in class to handle each request in a new process.""" |
| 282 | |
| 283 | active_children = None |
Guido van Rossum | 2ab455a | 1999-07-28 21:39:28 +0000 | [diff] [blame] | 284 | max_children = 40 |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 285 | |
| 286 | def collect_children(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 287 | """Internal routine to wait for died children.""" |
| 288 | while self.active_children: |
Guido van Rossum | 2ab455a | 1999-07-28 21:39:28 +0000 | [diff] [blame] | 289 | if len(self.active_children) < self.max_children: |
| 290 | options = os.WNOHANG |
| 291 | else: |
| 292 | # If the maximum number of children are already |
| 293 | # running, block while waiting for a child to exit |
| 294 | options = 0 |
Guido van Rossum | bfadac0 | 1999-06-17 15:41:33 +0000 | [diff] [blame] | 295 | try: |
Guido van Rossum | 2ab455a | 1999-07-28 21:39:28 +0000 | [diff] [blame] | 296 | pid, status = os.waitpid(0, options) |
Guido van Rossum | bfadac0 | 1999-06-17 15:41:33 +0000 | [diff] [blame] | 297 | except os.error: |
| 298 | pid = None |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 299 | if not pid: break |
| 300 | self.active_children.remove(pid) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 301 | |
| 302 | def process_request(self, request, client_address): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 303 | """Fork a new subprocess to process the request.""" |
| 304 | self.collect_children() |
| 305 | pid = os.fork() |
| 306 | if pid: |
| 307 | # Parent process |
| 308 | if self.active_children is None: |
| 309 | self.active_children = [] |
| 310 | self.active_children.append(pid) |
| 311 | return |
| 312 | else: |
| 313 | # Child process. |
| 314 | # This must never return, hence os._exit()! |
| 315 | try: |
Guido van Rossum | 198e7ca | 1999-06-01 18:58:34 +0000 | [diff] [blame] | 316 | self.socket.close() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 317 | self.finish_request(request, client_address) |
| 318 | os._exit(0) |
| 319 | except: |
| 320 | try: |
| 321 | self.handle_error(request, |
| 322 | client_address) |
| 323 | finally: |
| 324 | os._exit(1) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 325 | |
| 326 | |
| 327 | class ThreadingMixIn: |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 328 | """Mix-in class to handle each request in a new thread.""" |
| 329 | |
| 330 | def process_request(self, request, client_address): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 331 | """Start a new thread to process the request.""" |
Jeremy Hylton | 7526027 | 1999-10-12 16:20:13 +0000 | [diff] [blame] | 332 | import threading |
| 333 | t = threading.Thread(target = self.finish_request, |
| 334 | args = (request, client_address)) |
| 335 | t.start() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 336 | |
| 337 | |
| 338 | class ForkingUDPServer(ForkingMixIn, UDPServer): pass |
| 339 | class ForkingTCPServer(ForkingMixIn, TCPServer): pass |
| 340 | |
| 341 | class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
| 342 | class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass |
| 343 | |
Guido van Rossum | 67a40e8 | 1998-11-30 15:07:01 +0000 | [diff] [blame] | 344 | if hasattr(socket, 'AF_UNIX'): |
| 345 | |
| 346 | class UnixStreamServer(TCPServer): |
| 347 | address_family = socket.AF_UNIX |
| 348 | |
| 349 | class UnixDatagramServer(UDPServer): |
| 350 | address_family = socket.AF_UNIX |
| 351 | |
| 352 | class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer): pass |
| 353 | |
| 354 | class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 355 | |
| 356 | class BaseRequestHandler: |
| 357 | |
| 358 | """Base class for request handler classes. |
| 359 | |
| 360 | This class is instantiated for each request to be handled. The |
| 361 | constructor sets the instance variables request, client_address |
| 362 | and server, and then calls the handle() method. To implement a |
| 363 | specific service, all you need to do is to derive a class which |
| 364 | defines a handle() method. |
| 365 | |
| 366 | The handle() method can find the request as self.request, the |
Guido van Rossum | fdb3d1a | 1998-11-16 19:06:30 +0000 | [diff] [blame] | 367 | client address as self.client_address, and the server (in case it |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 368 | needs access to per-server information) as self.server. Since a |
| 369 | separate instance is created for each request, the handle() method |
| 370 | can define arbitrary other instance variariables. |
| 371 | |
| 372 | """ |
| 373 | |
| 374 | def __init__(self, request, client_address, server): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 375 | self.request = request |
| 376 | self.client_address = client_address |
| 377 | self.server = server |
| 378 | try: |
| 379 | self.setup() |
| 380 | self.handle() |
| 381 | self.finish() |
| 382 | finally: |
| 383 | sys.exc_traceback = None # Help garbage collection |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 384 | |
| 385 | def setup(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 386 | pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 387 | |
| 388 | def __del__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 389 | pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 390 | |
| 391 | def handle(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 392 | pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 393 | |
| 394 | def finish(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 395 | pass |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | # The following two classes make it possible to use the same service |
| 399 | # class for stream or datagram servers. |
| 400 | # Each class sets up these instance variables: |
| 401 | # - rfile: a file object from which receives the request is read |
| 402 | # - wfile: a file object to which the reply is written |
| 403 | # When the handle() method returns, wfile is flushed properly |
| 404 | |
| 405 | |
| 406 | class StreamRequestHandler(BaseRequestHandler): |
| 407 | |
| 408 | """Define self.rfile and self.wfile for stream sockets.""" |
| 409 | |
| 410 | def setup(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 411 | self.connection = self.request |
| 412 | self.rfile = self.connection.makefile('rb', 0) |
| 413 | self.wfile = self.connection.makefile('wb', 0) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 414 | |
| 415 | def finish(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 416 | self.wfile.flush() |
Guido van Rossum | 1d5102c | 1998-04-03 16:49:52 +0000 | [diff] [blame] | 417 | self.wfile.close() |
| 418 | self.rfile.close() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 419 | |
| 420 | |
| 421 | class DatagramRequestHandler(BaseRequestHandler): |
| 422 | |
| 423 | """Define self.rfile and self.wfile for datagram sockets.""" |
| 424 | |
| 425 | def setup(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 426 | import StringIO |
| 427 | self.packet, self.socket = self.request |
| 428 | self.rfile = StringIO.StringIO(self.packet) |
| 429 | self.wfile = StringIO.StringIO(self.packet) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 430 | |
| 431 | def finish(self): |
Guido van Rossum | 3249082 | 1998-06-16 02:27:33 +0000 | [diff] [blame] | 432 | self.socket.sendto(self.wfile.getvalue(), self.client_address) |