Added new optional credentials argument to SMTPHandler.__init__, and smtp.login(...
[python.git] / Lib / SocketServer.py
blob5506aa5b8ea8815e04f159d8fa824e16b784effe
1 """Generic socket server classes.
3 This module tries to capture the various aspects of defining a server:
5 For socket-based servers:
7 - address family:
8 - AF_INET{,6}: IP (Internet Protocol) sockets (default)
9 - AF_UNIX: Unix domain sockets
10 - others, e.g. AF_DECNET are conceivable (see <socket.h>
11 - socket type:
12 - SOCK_STREAM (reliable stream, e.g. TCP)
13 - SOCK_DGRAM (datagrams, e.g. UDP)
15 For request-based servers (including socket-based):
17 - client address verification before further looking at the request
18 (This is actually a hook for any processing that needs to look
19 at the request before anything else, e.g. logging)
20 - how to handle multiple requests:
21 - synchronous (one request is handled at a time)
22 - forking (each request is handled by a new process)
23 - threading (each request is handled by a new thread)
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.)
30 There are five classes in an inheritance diagram, four of which represent
31 synchronous servers of four types:
33 +------------+
34 | BaseServer |
35 +------------+
38 +-----------+ +------------------+
39 | TCPServer |------->| UnixStreamServer |
40 +-----------+ +------------------+
43 +-----------+ +--------------------+
44 | UDPServer |------->| UnixDatagramServer |
45 +-----------+ +--------------------+
47 Note that UnixDatagramServer derives from UDPServer, not from
48 UnixStreamServer -- the only difference between an IP and a Unix
49 stream server is the address family, which is simply repeated in both
50 unix server classes.
52 Forking and threading versions of each type of server can be created
53 using the ForkingMixIn and ThreadingMixIn mix-in classes. For
54 instance, a threading UDP server class is created as follows:
56 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
58 The Mix-in class must come first, since it overrides a method defined
59 in UDPServer! Setting the various member variables also changes
60 the behavior of the underlying server mechanism.
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.
67 The request handler class must be different for datagram or stream
68 services. This can be hidden by using the request handler
69 subclasses StreamRequestHandler or DatagramRequestHandler.
71 Of course, you still have to use your head!
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.
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.
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
91 server and doing an explicit fork in the request handler class
92 handle() method.
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
101 threads or subprocesses cannot be used).
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
109 XXX Open problems:
110 - What to do with out-of-band data?
112 BaseServer:
113 - split generic "request" functionality out into BaseServer class.
114 Copyright (C) 2000 Luke Kenneth Casson Leighton <lkcl@samba.org>
116 example: read entries from a SQL database (requires overriding
117 get_request() to return a table entry from the database).
118 entry is processed by a RequestHandlerClass.
122 # Author of the BaseServer patch: Luke Kenneth Casson Leighton
124 # XXX Warning!
125 # There is a test suite for this module, but it cannot be run by the
126 # standard regression test.
127 # To run it manually, run Lib/test/test_socketserver.py.
129 __version__ = "0.4"
132 import socket
133 import sys
134 import os
136 __all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer",
137 "ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler",
138 "StreamRequestHandler","DatagramRequestHandler",
139 "ThreadingMixIn", "ForkingMixIn"]
140 if hasattr(socket, "AF_UNIX"):
141 __all__.extend(["UnixStreamServer","UnixDatagramServer",
142 "ThreadingUnixStreamServer",
143 "ThreadingUnixDatagramServer"])
145 class BaseServer:
147 """Base class for server classes.
149 Methods for the caller:
151 - __init__(server_address, RequestHandlerClass)
152 - serve_forever()
153 - handle_request() # if you do not use serve_forever()
154 - fileno() -> int # for select()
156 Methods that may be overridden:
158 - server_bind()
159 - server_activate()
160 - get_request() -> request, client_address
161 - verify_request(request, client_address)
162 - server_close()
163 - process_request(request, client_address)
164 - close_request(request)
165 - handle_error()
167 Methods for derived classes:
169 - finish_request(request, client_address)
171 Class variables that may be overridden by derived classes or
172 instances:
174 - address_family
175 - socket_type
176 - allow_reuse_address
178 Instance variables:
180 - RequestHandlerClass
181 - socket
185 def __init__(self, server_address, RequestHandlerClass):
186 """Constructor. May be extended, do not override."""
187 self.server_address = server_address
188 self.RequestHandlerClass = RequestHandlerClass
190 def server_activate(self):
191 """Called by constructor to activate the server.
193 May be overridden.
196 pass
198 def serve_forever(self):
199 """Handle one request at a time until doomsday."""
200 while 1:
201 self.handle_request()
203 # The distinction between handling, getting, processing and
204 # finishing a request is fairly arbitrary. Remember:
206 # - handle_request() is the top-level call. It calls
207 # get_request(), verify_request() and process_request()
208 # - get_request() is different for stream or datagram sockets
209 # - process_request() is the place that may fork a new process
210 # or create a new thread to finish the request
211 # - finish_request() instantiates the request handler class;
212 # this constructor will handle the request all by itself
214 def handle_request(self):
215 """Handle one request, possibly blocking."""
216 try:
217 request, client_address = self.get_request()
218 except socket.error:
219 return
220 if self.verify_request(request, client_address):
221 try:
222 self.process_request(request, client_address)
223 except:
224 self.handle_error(request, client_address)
225 self.close_request(request)
227 def verify_request(self, request, client_address):
228 """Verify the request. May be overridden.
230 Return True if we should proceed with this request.
233 return True
235 def process_request(self, request, client_address):
236 """Call finish_request.
238 Overridden by ForkingMixIn and ThreadingMixIn.
241 self.finish_request(request, client_address)
242 self.close_request(request)
244 def server_close(self):
245 """Called to clean-up the server.
247 May be overridden.
250 pass
252 def finish_request(self, request, client_address):
253 """Finish one request by instantiating RequestHandlerClass."""
254 self.RequestHandlerClass(request, client_address, self)
256 def close_request(self, request):
257 """Called to clean up an individual request."""
258 pass
260 def handle_error(self, request, client_address):
261 """Handle an error gracefully. May be overridden.
263 The default is to print a traceback and continue.
266 print '-'*40
267 print 'Exception happened during processing of request from',
268 print client_address
269 import traceback
270 traceback.print_exc() # XXX But this goes to stderr!
271 print '-'*40
274 class TCPServer(BaseServer):
276 """Base class for various socket-based server classes.
278 Defaults to synchronous IP stream (i.e., TCP).
280 Methods for the caller:
282 - __init__(server_address, RequestHandlerClass, bind_and_activate=True)
283 - serve_forever()
284 - handle_request() # if you don't use serve_forever()
285 - fileno() -> int # for select()
287 Methods that may be overridden:
289 - server_bind()
290 - server_activate()
291 - get_request() -> request, client_address
292 - verify_request(request, client_address)
293 - process_request(request, client_address)
294 - close_request(request)
295 - handle_error()
297 Methods for derived classes:
299 - finish_request(request, client_address)
301 Class variables that may be overridden by derived classes or
302 instances:
304 - address_family
305 - socket_type
306 - request_queue_size (only for stream sockets)
307 - allow_reuse_address
309 Instance variables:
311 - server_address
312 - RequestHandlerClass
313 - socket
317 address_family = socket.AF_INET
319 socket_type = socket.SOCK_STREAM
321 request_queue_size = 5
323 allow_reuse_address = False
325 def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
326 """Constructor. May be extended, do not override."""
327 BaseServer.__init__(self, server_address, RequestHandlerClass)
328 self.socket = socket.socket(self.address_family,
329 self.socket_type)
330 if bind_and_activate:
331 self.server_bind()
332 self.server_activate()
334 def server_bind(self):
335 """Called by constructor to bind the socket.
337 May be overridden.
340 if self.allow_reuse_address:
341 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
342 self.socket.bind(self.server_address)
343 self.server_address = self.socket.getsockname()
345 def server_activate(self):
346 """Called by constructor to activate the server.
348 May be overridden.
351 self.socket.listen(self.request_queue_size)
353 def server_close(self):
354 """Called to clean-up the server.
356 May be overridden.
359 self.socket.close()
361 def fileno(self):
362 """Return socket file number.
364 Interface required by select().
367 return self.socket.fileno()
369 def get_request(self):
370 """Get the request and client address from the socket.
372 May be overridden.
375 return self.socket.accept()
377 def close_request(self, request):
378 """Called to clean up an individual request."""
379 request.close()
382 class UDPServer(TCPServer):
384 """UDP server class."""
386 allow_reuse_address = False
388 socket_type = socket.SOCK_DGRAM
390 max_packet_size = 8192
392 def get_request(self):
393 data, client_addr = self.socket.recvfrom(self.max_packet_size)
394 return (data, self.socket), client_addr
396 def server_activate(self):
397 # No need to call listen() for UDP.
398 pass
400 def close_request(self, request):
401 # No need to close anything.
402 pass
404 class ForkingMixIn:
406 """Mix-in class to handle each request in a new process."""
408 active_children = None
409 max_children = 40
411 def collect_children(self):
412 """Internal routine to wait for died children."""
413 while self.active_children:
414 if len(self.active_children) < self.max_children:
415 options = os.WNOHANG
416 else:
417 # If the maximum number of children are already
418 # running, block while waiting for a child to exit
419 options = 0
420 try:
421 pid, status = os.waitpid(0, options)
422 except os.error:
423 pid = None
424 if not pid: break
425 self.active_children.remove(pid)
427 def process_request(self, request, client_address):
428 """Fork a new subprocess to process the request."""
429 self.collect_children()
430 pid = os.fork()
431 if pid:
432 # Parent process
433 if self.active_children is None:
434 self.active_children = []
435 self.active_children.append(pid)
436 self.close_request(request)
437 return
438 else:
439 # Child process.
440 # This must never return, hence os._exit()!
441 try:
442 self.finish_request(request, client_address)
443 os._exit(0)
444 except:
445 try:
446 self.handle_error(request, client_address)
447 finally:
448 os._exit(1)
451 class ThreadingMixIn:
452 """Mix-in class to handle each request in a new thread."""
454 # Decides how threads will act upon termination of the
455 # main process
456 daemon_threads = False
458 def process_request_thread(self, request, client_address):
459 """Same as in BaseServer but as a thread.
461 In addition, exception handling is done here.
464 try:
465 self.finish_request(request, client_address)
466 self.close_request(request)
467 except:
468 self.handle_error(request, client_address)
469 self.close_request(request)
471 def process_request(self, request, client_address):
472 """Start a new thread to process the request."""
473 import threading
474 t = threading.Thread(target = self.process_request_thread,
475 args = (request, client_address))
476 if self.daemon_threads:
477 t.setDaemon (1)
478 t.start()
481 class ForkingUDPServer(ForkingMixIn, UDPServer): pass
482 class ForkingTCPServer(ForkingMixIn, TCPServer): pass
484 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
485 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
487 if hasattr(socket, 'AF_UNIX'):
489 class UnixStreamServer(TCPServer):
490 address_family = socket.AF_UNIX
492 class UnixDatagramServer(UDPServer):
493 address_family = socket.AF_UNIX
495 class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer): pass
497 class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass
499 class BaseRequestHandler:
501 """Base class for request handler classes.
503 This class is instantiated for each request to be handled. The
504 constructor sets the instance variables request, client_address
505 and server, and then calls the handle() method. To implement a
506 specific service, all you need to do is to derive a class which
507 defines a handle() method.
509 The handle() method can find the request as self.request, the
510 client address as self.client_address, and the server (in case it
511 needs access to per-server information) as self.server. Since a
512 separate instance is created for each request, the handle() method
513 can define arbitrary other instance variariables.
517 def __init__(self, request, client_address, server):
518 self.request = request
519 self.client_address = client_address
520 self.server = server
521 try:
522 self.setup()
523 self.handle()
524 self.finish()
525 finally:
526 sys.exc_traceback = None # Help garbage collection
528 def setup(self):
529 pass
531 def handle(self):
532 pass
534 def finish(self):
535 pass
538 # The following two classes make it possible to use the same service
539 # class for stream or datagram servers.
540 # Each class sets up these instance variables:
541 # - rfile: a file object from which receives the request is read
542 # - wfile: a file object to which the reply is written
543 # When the handle() method returns, wfile is flushed properly
546 class StreamRequestHandler(BaseRequestHandler):
548 """Define self.rfile and self.wfile for stream sockets."""
550 # Default buffer sizes for rfile, wfile.
551 # We default rfile to buffered because otherwise it could be
552 # really slow for large data (a getc() call per byte); we make
553 # wfile unbuffered because (a) often after a write() we want to
554 # read and we need to flush the line; (b) big writes to unbuffered
555 # files are typically optimized by stdio even when big reads
556 # aren't.
557 rbufsize = -1
558 wbufsize = 0
560 def setup(self):
561 self.connection = self.request
562 self.rfile = self.connection.makefile('rb', self.rbufsize)
563 self.wfile = self.connection.makefile('wb', self.wbufsize)
565 def finish(self):
566 if not self.wfile.closed:
567 self.wfile.flush()
568 self.wfile.close()
569 self.rfile.close()
572 class DatagramRequestHandler(BaseRequestHandler):
574 # XXX Regrettably, I cannot get this working on Linux;
575 # s.recvfrom() doesn't return a meaningful client address.
577 """Define self.rfile and self.wfile for datagram sockets."""
579 def setup(self):
580 try:
581 from cStringIO import StringIO
582 except ImportError:
583 from StringIO import StringIO
584 self.packet, self.socket = self.request
585 self.rfile = StringIO(self.packet)
586 self.wfile = StringIO()
588 def finish(self):
589 self.socket.sendto(self.wfile.getvalue(), self.client_address)