annotate Lib/SocketServer.py @ 12357:5b59ea41db4e legacy-trunk

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