annotate Lib/dos_8x3/socketse.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 569e3af52187
children da3c7d47f210
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3784
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Generic socket server classes.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 This module tries to capture the various aspects of defining a server:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 - address family:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 - AF_INET: IP (Internet Protocol) sockets (default)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 - AF_UNIX: Unix domain sockets
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 - others, e.g. AF_DECNET are conceivable (see <socket.h>
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 - socket type:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 - SOCK_STREAM (reliable stream, e.g. TCP)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 - SOCK_DGRAM (datagrams, e.g. UDP)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 - client address verification before further looking at the request
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 (This is actually a hook for any processing that needs to look
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 at the request before anything else, e.g. logging)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 - how to handle multiple requests:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 - synchronous (one request is handled at a time)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 - forking (each request is handled by a new process)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 - threading (each request is handled by a new thread)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 The classes in this module favor the server type that is simplest to
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 write: a synchronous TCP/IP server. This is bad class design, but
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 save some typing. (There's also the issue that a deep class hierarchy
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 slows down method lookups.)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 There are four classes in an inheritance diagram that represent
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 synchronous servers of four types:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 +-----------+ +------------------+
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 | TCPServer |------->| UnixStreamServer |
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 +-----------+ +------------------+
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 |
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 v
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 +-----------+ +--------------------+
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 | UDPServer |------->| UnixDatagramServer |
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 +-----------+ +--------------------+
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 (Note that UnixDatagramServer derives from UDPServer, not from
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 UnixStreamServer -- the only difference between an IP and a Unix
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 stream server is the address family, which is simply repeated in both
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 unix server classes.)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 Forking and threading versions of each type of server can be created
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 using the ForkingServer and ThreadingServer mix-in classes. For
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 instance, a threading UDP server class is created as follows:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 (The Mix-in class must come first, since it overrides a method defined
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 in UDPServer!)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 To implement a service, you must derive a class from
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 BaseRequestHandler and redefine its handle() method. You can then run
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 various versions of the service by combining one of the server classes
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 with your request handler class.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 The request handler class must be different for datagram or stream
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 services. This can be hidden by using the mix-in request handler
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 classes StreamRequestHandler or DatagramRequestHandler.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 Of course, you still have to use your head!
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 For instance, it makes no sense to use a forking server if the service
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 contains state in memory that can be modified by requests (since the
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 modifications in the child process would never reach the initial state
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 kept in the parent process and passed to each child). In this case,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 you can use a threading server, but you will probably have to use
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 locks to avoid two requests that come in nearly simultaneous to apply
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 conflicting changes to the server state.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
b41019557b6e Fuck. For PC support, this must be in the distribution.
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
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 data is stored externally (e.g. in the file system), a synchronous
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 class will essentially render the service "deaf" while one request is
b41019557b6e Fuck. For PC support, this must be in the distribution.
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
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 to reqd all the data it has requested. Here a threading or forking
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 server is appropriate.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 In some cases, it may be appropriate to process part of a request
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 synchronously, but to finish processing in a forked child depending on
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 the request data. This can be implemented by using a synchronous
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 server and doing an explicit fork in the request handler class's
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 handle() method.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 Another approach to handling multiple simultaneous requests in an
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 environment that supports neither threads nor fork (or where these are
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 too expensive or inappropriate for the service) is to maintain an
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 explicit table of partially finished requests and to use select() to
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 decide which request to work on next (or whether to handle a new
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 incoming request). This is particularly important for stream services
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 where each client can potentially be connected for a long time (if
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 threads or subprocesses can't be used).
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 Future work:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 - Standard classes for Sun RPC (which uses either UDP or TCP)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 - Standard mix-in classes to implement various authentication
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 and encryption schemes
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 - Standard framework for select-based multiplexing
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 XXX Open problems:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 - What to do with out-of-band data?
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 __version__ = "0.2"
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 import socket
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 import sys
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 import os
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 class TCPServer:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 """Base class for various socket-based server classes.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 Defaults to synchronous IP stream (i.e., TCP).
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 Methods for the caller:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 - __init__(server_address, RequestHandlerClass)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 - serve_forever()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 - handle_request() # if you don't use serve_forever()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 - fileno() -> int # for select()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 Methods that may be overridden:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 - server_bind()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 - server_activate()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 - get_request() -> request, client_address
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 - verify_request(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 - process_request(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 - handle_error()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 Methods for derived classes:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 - finish_request(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 Class variables that may be overridden by derived classes or
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 instances:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 - address_family
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 - socket_type
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 - request_queue_size (only for stream sockets)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 Instance variables:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 - server_address
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 - RequestHandlerClass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 - socket
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 address_family = socket.AF_INET
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 socket_type = socket.SOCK_STREAM
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 request_queue_size = 5
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 def __init__(self, server_address, RequestHandlerClass):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 """Constructor. May be extended, do not override."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 self.server_address = server_address
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 self.RequestHandlerClass = RequestHandlerClass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 self.socket = socket.socket(self.address_family,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 self.socket_type)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 self.server_bind()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 self.server_activate()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def server_bind(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 """Called by constructor to bind the socket.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 May be overridden.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 self.socket.bind(self.server_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 def server_activate(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 """Called by constructor to activate the server.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 May be overridden.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 self.socket.listen(self.request_queue_size)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 def fileno(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 """Return socket file number.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 Interface required by select().
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 return self.socket.fileno()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 def serve_forever(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 """Handle one request at a time until doomsday."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 while 1:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 self.handle_request()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 # The distinction between handling, getting, processing and
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 # finishing a request is fairly arbitrary. Remember:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 #
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 # - handle_request() is the top-level call. It calls
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 # get_request(), verify_request() and process_request()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 # - get_request() is different for stream or datagram sockets
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 # - process_request() is the place that may fork a new process
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 # or create a new thread to finish the request
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 # - finish_request() instantiates the request handler class;
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 # this constructor will handle the request all by itself
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 def handle_request(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 """Handle one request, possibly blocking."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 request, client_address = self.get_request()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 if self.verify_request(request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 self.process_request(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 except:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 self.handle_error(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 def get_request(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 """Get the request and client address from the socket.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 May be overridden.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 return self.socket.accept()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 def verify_request(self, request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 """Verify the request. May be overridden.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 Return true if we should proceed with this request.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 return 1
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
232
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 def process_request(self, request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 """Call finish_request.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 Overridden by ForkingMixIn and ThreadingMixIn.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 self.finish_request(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
240
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 def finish_request(self, request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 """Finish one request by instantiating RequestHandlerClass."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 self.RequestHandlerClass(request, client_address, self)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def handle_error(self, request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 """Handle an error gracefully. May be overridden.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 The default is to print a traceback and continue.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 exc, value, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 print '-'*40
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 print 'Exception happened during processing of request from',
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 print client_address
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 import traceback
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 traceback.print_exception(exc, value, tb)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 print '-'*40
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
259
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 class UDPServer(TCPServer):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 """UDP server class."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
263
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 socket_type = socket.SOCK_DGRAM
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 max_packet_size = 8192
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 def get_request(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 return self.socket.recvfrom(max_packet_size)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 if hasattr(socket, 'AF_UNIX'):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 class UnixStreamServer(TCPServer):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 address_family = socket.AF_UNIX
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 class UnixDatagramServer(UDPServer):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 address_family = socket.AF_UNIX
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
282
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
283
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 class ForkingMixIn:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 """Mix-in class to handle each request in a new process."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 active_children = None
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
289
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 def collect_children(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 """Internal routine to wait for died children."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 while self.active_children:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 pid, status = os.waitpid(0, os.WNOHANG)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 if not pid: break
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 self.active_children.remove(pid)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 def process_request(self, request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 """Fork a new subprocess to process the request."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 self.collect_children()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 pid = os.fork()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 if pid:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 # Parent process
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 if self.active_children is None:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 self.active_children = []
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 self.active_children.append(pid)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 return
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 # Child process.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 # This must never return, hence os._exit()!
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 self.finish_request(request, client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 os._exit(0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 except:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 self.handle_error(request,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 client_address)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 finally:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 os._exit(1)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 class ThreadingMixIn:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 """Mix-in class to handle each request in a new thread."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
324
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 def process_request(self, request, client_address):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 """Start a new thread to process the request."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 import thread
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 thread.start_new_thread(self.finish_request,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 (request, client_address))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
331
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 class ForkingUDPServer(ForkingMixIn, UDPServer): pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 class ForkingTCPServer(ForkingMixIn, TCPServer): pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
334
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
338
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 class BaseRequestHandler:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 """Base class for request handler classes.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
342
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 This class is instantiated for each request to be handled. The
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 constructor sets the instance variables request, client_address
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 and server, and then calls the handle() method. To implement a
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 specific service, all you need to do is to derive a class which
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 defines a handle() method.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 The handle() method can find the request as self.request, the
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 client address as self.client_request, and the server (in case it
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 needs access to per-server information) as self.server. Since a
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 separate instance is created for each request, the handle() method
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 can define arbitrary other instance variariables.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 def __init__(self, request, client_address, server):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 self.request = request
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 self.client_address = client_address
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 self.server = server
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 try:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 self.setup()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 self.handle()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 self.finish()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 finally:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 sys.exc_traceback = None # Help garbage collection
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 def setup(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 def __del__(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 def handle(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 def finish(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
379
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 # The following two classes make it possible to use the same service
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 # class for stream or datagram servers.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 # Each class sets up these instance variables:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 # - rfile: a file object from which receives the request is read
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 # - wfile: a file object to which the reply is written
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 # When the handle() method returns, wfile is flushed properly
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 class StreamRequestHandler(BaseRequestHandler):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 """Define self.rfile and self.wfile for stream sockets."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 def setup(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 self.connection = self.request
4495
569e3af52187 The usual :-(
Guido van Rossum <guido@python.org>
parents: 3784
diff changeset
395 self.rfile = self.connection.makefile('rb', 0)
3784
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 self.wfile = self.connection.makefile('wb', 0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 def finish(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 self.wfile.flush()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 class DatagramRequestHandler(BaseRequestHandler):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 """Define self.rfile and self.wfile for datagram sockets."""
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
405
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 def setup(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 import StringIO
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 self.packet, self.socket = self.request
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 self.rfile = StringIO.StringIO(self.packet)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 self.wfile = StringIO.StringIO(self.packet)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 def finish(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 self.socket.send(self.wfile.getvalue())