Mercurial > cpython-lru_cache-weakref
annotate Lib/SocketServer.py @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | b1ee4321a50a |
| children | a6de50c0d457 |
| rev | line source |
|---|---|
| 2887 | 1 """Generic socket server classes. |
| 2 | |
| 3 This module tries to capture the various aspects of defining a server: | |
| 4 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
5 For socket-based servers: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
6 |
| 2887 | 7 - address family: |
|
18825
40900620293e
Patch #401196: Use getaddrinfo and AF_INET6 in TCP servers and clients.
Martin v. Löwis <martin@v.loewis.de>
parents:
18471
diff
changeset
|
8 - AF_INET{,6}: IP (Internet Protocol) sockets (default) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
9 - 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
|
10 - others, e.g. AF_DECNET are conceivable (see <socket.h> |
| 2887 | 11 - socket type: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
12 - 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
|
13 - SOCK_DGRAM (datagrams, e.g. UDP) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
14 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
15 For request-based servers (including socket-based): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
16 |
| 2887 | 17 - 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
|
18 (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
|
19 at the request before anything else, e.g. logging) |
| 2887 | 20 - 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
|
21 - 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
|
22 - 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
|
23 - threading (each request is handled by a new thread) |
| 2887 | 24 |
| 25 The classes in this module favor the server type that is simplest to | |
| 26 write: a synchronous TCP/IP server. This is bad class design, but | |
| 27 save some typing. (There's also the issue that a deep class hierarchy | |
| 28 slows down method lookups.) | |
| 29 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
30 There are five classes in an inheritance diagram, four of which represent |
| 2887 | 31 synchronous servers of four types: |
| 32 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
33 +------------+ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
34 | BaseServer | |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
35 +------------+ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
36 | |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
37 v |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
38 +-----------+ +------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
39 | TCPServer |------->| UnixStreamServer | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
40 +-----------+ +------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
41 | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
42 v |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
43 +-----------+ +--------------------+ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
44 | UDPServer |------->| UnixDatagramServer | |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
45 +-----------+ +--------------------+ |
| 2887 | 46 |
|
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
|
47 Note that UnixDatagramServer derives from UDPServer, not from |
| 2887 | 48 UnixStreamServer -- the only difference between an IP and a Unix |
| 49 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
|
50 unix server classes. |
| 2887 | 51 |
| 52 Forking and threading versions of each type of server can be created | |
| 53 using the ForkingServer and ThreadingServer mix-in classes. For | |
| 54 instance, a threading UDP server class is created as follows: | |
| 55 | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
56 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
| 2887 | 57 |
|
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
|
58 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
|
59 in UDPServer! |
| 2887 | 60 |
| 61 To implement a service, you must derive a class from | |
| 62 BaseRequestHandler and redefine its handle() method. You can then run | |
| 63 various versions of the service by combining one of the server classes | |
| 64 with your request handler class. | |
| 65 | |
| 66 The request handler class must be different for datagram or stream | |
| 67 services. This can be hidden by using the mix-in request handler | |
| 68 classes StreamRequestHandler or DatagramRequestHandler. | |
| 69 | |
| 70 Of course, you still have to use your head! | |
| 71 | |
| 72 For instance, it makes no sense to use a forking server if the service | |
| 73 contains state in memory that can be modified by requests (since the | |
| 74 modifications in the child process would never reach the initial state | |
| 75 kept in the parent process and passed to each child). In this case, | |
| 76 you can use a threading server, but you will probably have to use | |
| 77 locks to avoid two requests that come in nearly simultaneous to apply | |
| 78 conflicting changes to the server state. | |
| 79 | |
| 80 On the other hand, if you are building e.g. an HTTP server, where all | |
| 81 data is stored externally (e.g. in the file system), a synchronous | |
| 82 class will essentially render the service "deaf" while one request is | |
| 83 being handled -- which may be for a very long time if a client is slow | |
| 84 to reqd all the data it has requested. Here a threading or forking | |
| 85 server is appropriate. | |
| 86 | |
| 87 In some cases, it may be appropriate to process part of a request | |
| 88 synchronously, but to finish processing in a forked child depending on | |
| 89 the request data. This can be implemented by using a synchronous | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
90 server and doing an explicit fork in the request handler class |
| 2887 | 91 handle() method. |
| 92 | |
| 93 Another approach to handling multiple simultaneous requests in an | |
| 94 environment that supports neither threads nor fork (or where these are | |
| 95 too expensive or inappropriate for the service) is to maintain an | |
| 96 explicit table of partially finished requests and to use select() to | |
| 97 decide which request to work on next (or whether to handle a new | |
| 98 incoming request). This is particularly important for stream services | |
| 99 where each client can potentially be connected for a long time (if | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
100 threads or subprocesses cannot be used). |
| 2887 | 101 |
| 102 Future work: | |
| 103 - Standard classes for Sun RPC (which uses either UDP or TCP) | |
| 104 - Standard mix-in classes to implement various authentication | |
| 105 and encryption schemes | |
| 106 - Standard framework for select-based multiplexing | |
| 107 | |
| 108 XXX Open problems: | |
| 109 - What to do with out-of-band data? | |
| 110 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
111 BaseServer: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
112 - split generic "request" functionality out into BaseServer class. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
113 Copyright (C) 2000 Luke Kenneth Casson Leighton <lkcl@samba.org> |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
114 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
115 example: read entries from a SQL database (requires overriding |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
116 get_request() to return a table entry from the database). |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
117 entry is processed by a RequestHandlerClass. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
118 |
| 2887 | 119 """ |
| 120 | |
|
16037
ba295e2c73ea
Bump __version__; add authorship note for the BaseServer patch.
Guido van Rossum <guido@python.org>
parents:
16010
diff
changeset
|
121 # Author of the BaseServer patch: Luke Kenneth Casson Leighton |
| 2887 | 122 |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
123 # XXX Warning! |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
124 # There is a test suite for this module, but it cannot be run by the |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
125 # standard regression test. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
126 # To run it manually, run Lib/test/test_socketserver.py. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
127 |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
128 __version__ = "0.4" |
| 2887 | 129 |
| 130 | |
| 131 import socket | |
| 132 import sys | |
| 133 import os | |
| 134 | |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
135 __all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
136 "ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler", |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
137 "StreamRequestHandler","DatagramRequestHandler", |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
138 "ThreadingMixIn", "ForkingMixIn"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
139 if hasattr(socket, "AF_UNIX"): |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
140 __all__.extend(["UnixStreamServer","UnixDatagramServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
141 "ThreadingUnixStreamServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
142 "ThreadingUnixDatagramServer"]) |
| 2887 | 143 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
144 class BaseServer: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
145 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
146 """Base class for server classes. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
147 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
148 Methods for the caller: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
149 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
150 - __init__(server_address, RequestHandlerClass) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
151 - serve_forever() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
152 - handle_request() # if you do not use serve_forever() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
153 - fileno() -> int # for select() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
154 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
155 Methods that may be overridden: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
156 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
157 - server_bind() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
158 - server_activate() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
159 - get_request() -> request, client_address |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
160 - verify_request(request, client_address) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
161 - server_close() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
162 - process_request(request, client_address) |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
163 - close_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
164 - handle_error() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
165 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
166 Methods for derived classes: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
167 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
168 - finish_request(request, client_address) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
169 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
170 Class variables that may be overridden by derived classes or |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
171 instances: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
172 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
173 - address_family |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
174 - socket_type |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
175 - reuse_address |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
176 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
177 Instance variables: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
178 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
179 - RequestHandlerClass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
180 - socket |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
181 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
182 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
183 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
184 def __init__(self, server_address, RequestHandlerClass): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
185 """Constructor. May be extended, do not override.""" |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
186 self.server_address = server_address |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
187 self.RequestHandlerClass = RequestHandlerClass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
188 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
189 def server_activate(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
190 """Called by constructor to activate the server. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
191 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
192 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
193 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
194 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
195 pass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
196 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
197 def serve_forever(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
198 """Handle one request at a time until doomsday.""" |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
199 while 1: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
200 self.handle_request() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
201 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
202 # The distinction between handling, getting, processing and |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
203 # finishing a request is fairly arbitrary. Remember: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
204 # |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
205 # - handle_request() is the top-level call. It calls |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
206 # get_request(), verify_request() and process_request() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
207 # - get_request() is different for stream or datagram sockets |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
208 # - process_request() is the place that may fork a new process |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
209 # or create a new thread to finish the request |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
210 # - finish_request() instantiates the request handler class; |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
211 # this constructor will handle the request all by itself |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
212 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
213 def handle_request(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
214 """Handle one request, possibly blocking.""" |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
215 try: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
216 request, client_address = self.get_request() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
217 except socket.error: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
218 return |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
219 if self.verify_request(request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
220 try: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
221 self.process_request(request, client_address) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
222 except: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
223 self.handle_error(request, client_address) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
224 self.close_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
225 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
226 def verify_request(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
227 """Verify the request. May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
228 |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20636
diff
changeset
|
229 Return True if we should proceed with this request. |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
230 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
231 """ |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20636
diff
changeset
|
232 return True |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
233 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
234 def process_request(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
235 """Call finish_request. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
236 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
237 Overridden by ForkingMixIn and ThreadingMixIn. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
238 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
239 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
240 self.finish_request(request, client_address) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
241 self.close_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
242 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
243 def server_close(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
244 """Called to clean-up the server. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
245 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
246 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
247 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
248 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
249 pass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
250 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
251 def finish_request(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
252 """Finish one request by instantiating RequestHandlerClass.""" |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
253 self.RequestHandlerClass(request, client_address, self) |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
254 |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
255 def close_request(self, request): |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
256 """Called to clean up an individual request.""" |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
257 pass |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
258 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
259 def handle_error(self, request, client_address): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
260 """Handle an error gracefully. May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
261 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
262 The default is to print a traceback and continue. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
263 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
264 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
265 print '-'*40 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
266 print 'Exception happened during processing of request from', |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
267 print client_address |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
268 import traceback |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
269 traceback.print_exc() # XXX But this goes to stderr! |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
270 print '-'*40 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
271 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
272 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
273 class TCPServer(BaseServer): |
| 2887 | 274 |
| 275 """Base class for various socket-based server classes. | |
| 276 | |
| 277 Defaults to synchronous IP stream (i.e., TCP). | |
| 278 | |
| 279 Methods for the caller: | |
| 280 | |
| 281 - __init__(server_address, RequestHandlerClass) | |
| 282 - serve_forever() | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
283 - 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
|
284 - fileno() -> int # for select() |
| 2887 | 285 |
| 286 Methods that may be overridden: | |
| 287 | |
| 288 - server_bind() | |
| 289 - server_activate() | |
| 290 - get_request() -> request, client_address | |
| 291 - verify_request(request, client_address) | |
| 292 - process_request(request, client_address) | |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
293 - close_request(request) |
| 2887 | 294 - handle_error() |
| 295 | |
| 296 Methods for derived classes: | |
| 297 | |
| 298 - finish_request(request, client_address) | |
| 299 | |
| 300 Class variables that may be overridden by derived classes or | |
| 301 instances: | |
| 302 | |
| 303 - address_family | |
| 304 - socket_type | |
| 305 - request_queue_size (only for stream sockets) | |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
306 - reuse_address |
| 2887 | 307 |
| 308 Instance variables: | |
| 309 | |
| 310 - server_address | |
| 311 - RequestHandlerClass | |
| 312 - socket | |
| 313 | |
| 314 """ | |
| 315 | |
| 316 address_family = socket.AF_INET | |
| 317 | |
| 318 socket_type = socket.SOCK_STREAM | |
| 319 | |
| 320 request_queue_size = 5 | |
| 321 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
322 allow_reuse_address = 0 |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
323 |
| 2887 | 324 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
|
325 """Constructor. May be extended, do not override.""" |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
326 BaseServer.__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
|
327 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
|
328 self.socket_type) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
329 self.server_bind() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
330 self.server_activate() |
| 2887 | 331 |
| 332 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
|
333 """Called by constructor to bind the socket. |
| 2887 | 334 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
335 May be overridden. |
| 2887 | 336 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
337 """ |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
338 if self.allow_reuse_address: |
|
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
339 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
340 self.socket.bind(self.server_address) |
| 2887 | 341 |
| 342 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
|
343 """Called by constructor to activate the server. |
| 2887 | 344 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
345 May be overridden. |
| 2887 | 346 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
347 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
348 self.socket.listen(self.request_queue_size) |
| 2887 | 349 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
350 def server_close(self): |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
351 """Called to clean-up the server. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
352 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
353 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
354 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
355 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
356 self.socket.close() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
357 |
| 2887 | 358 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
|
359 """Return socket file number. |
| 2887 | 360 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
361 Interface required by select(). |
| 2887 | 362 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
363 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
364 return self.socket.fileno() |
| 2887 | 365 |
| 366 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
|
367 """Get the request and client address from the socket. |
| 2887 | 368 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
369 May be overridden. |
| 2887 | 370 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
371 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
372 return self.socket.accept() |
| 2887 | 373 |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
374 def close_request(self, request): |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
375 """Called to clean up an individual request.""" |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
376 request.close() |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
377 |
| 2887 | 378 |
| 379 class UDPServer(TCPServer): | |
| 380 | |
| 381 """UDP server class.""" | |
| 382 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
383 allow_reuse_address = 0 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
384 |
| 2887 | 385 socket_type = socket.SOCK_DGRAM |
| 386 | |
| 387 max_packet_size = 8192 | |
| 388 | |
| 389 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
|
390 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
|
391 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
|
392 |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
393 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
|
394 # 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
|
395 pass |
| 2887 | 396 |
|
17400
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
397 def close_request(self, request): |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
398 # No need to close anything. |
|
13857fb33017
Add a close_request method to the BaseServer so that the TCPServer class
Ka-Ping Yee <ping@zesty.ca>
parents:
16070
diff
changeset
|
399 pass |
| 2887 | 400 |
| 401 class ForkingMixIn: | |
| 402 | |
| 403 """Mix-in class to handle each request in a new process.""" | |
| 404 | |
| 405 active_children = None | |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
406 max_children = 40 |
| 2887 | 407 |
| 408 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
|
409 """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
|
410 while self.active_children: |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
411 if len(self.active_children) < self.max_children: |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
412 options = os.WNOHANG |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
413 else: |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
414 # If the maximum number of children are already |
|
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
415 # 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
|
416 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
|
417 try: |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
418 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
|
419 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
|
420 pid = None |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
421 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
|
422 self.active_children.remove(pid) |
| 2887 | 423 |
| 424 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
|
425 """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
|
426 self.collect_children() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
427 pid = os.fork() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
428 if pid: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
429 # Parent process |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
430 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
|
431 self.active_children = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
432 self.active_children.append(pid) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
433 self.close_request(request) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
434 return |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
435 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
436 # Child process. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
437 # 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
|
438 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
439 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
|
440 os._exit(0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
441 except: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
442 try: |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
443 self.handle_error(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
|
444 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
445 os._exit(1) |
| 2887 | 446 |
| 447 | |
| 448 class ThreadingMixIn: | |
| 449 """Mix-in class to handle each request in a new thread.""" | |
| 450 | |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
451 def process_request_thread(self, request, client_address): |
|
20636
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
452 """Same as in BaseServer but as a thread. |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
453 |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
454 In addition, exception handling is done here. |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
455 |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
456 """ |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
457 try: |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
458 self.finish_request(request, client_address) |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
459 self.close_request(request) |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
460 except: |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
461 self.handle_error(request, client_address) |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
462 self.close_request(request) |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
463 |
| 2887 | 464 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
|
465 """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
|
466 import threading |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
467 t = threading.Thread(target = self.process_request_thread, |
|
11382
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
468 args = (request, client_address)) |
|
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
469 t.start() |
| 2887 | 470 |
| 471 | |
| 472 class ForkingUDPServer(ForkingMixIn, UDPServer): pass | |
| 473 class ForkingTCPServer(ForkingMixIn, TCPServer): pass | |
| 474 | |
| 475 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass | |
| 476 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass | |
| 477 | |
|
9544
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
478 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
|
479 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
480 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
|
481 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
|
482 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
483 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
|
484 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
|
485 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
486 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
|
487 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
488 class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass |
| 2887 | 489 |
| 490 class BaseRequestHandler: | |
| 491 | |
| 492 """Base class for request handler classes. | |
| 493 | |
| 494 This class is instantiated for each request to be handled. The | |
| 495 constructor sets the instance variables request, client_address | |
| 496 and server, and then calls the handle() method. To implement a | |
| 497 specific service, all you need to do is to derive a class which | |
| 498 defines a handle() method. | |
| 499 | |
| 500 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
|
501 client address as self.client_address, and the server (in case it |
| 2887 | 502 needs access to per-server information) as self.server. Since a |
| 503 separate instance is created for each request, the handle() method | |
| 504 can define arbitrary other instance variariables. | |
| 505 | |
| 506 """ | |
| 507 | |
| 508 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
|
509 self.request = request |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
510 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
|
511 self.server = server |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
512 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
513 self.setup() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
514 self.handle() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
515 self.finish() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
516 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
517 sys.exc_traceback = None # Help garbage collection |
| 2887 | 518 |
| 519 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
|
520 pass |
| 2887 | 521 |
| 522 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
|
523 pass |
| 2887 | 524 |
| 525 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
|
526 pass |
| 2887 | 527 |
| 528 | |
| 529 # The following two classes make it possible to use the same service | |
| 530 # class for stream or datagram servers. | |
| 531 # Each class sets up these instance variables: | |
| 532 # - rfile: a file object from which receives the request is read | |
| 533 # - wfile: a file object to which the reply is written | |
| 534 # When the handle() method returns, wfile is flushed properly | |
| 535 | |
| 536 | |
| 537 class StreamRequestHandler(BaseRequestHandler): | |
| 538 | |
| 539 """Define self.rfile and self.wfile for stream sockets.""" | |
| 540 | |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
541 # Default buffer sizes for rfile, wfile. |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
542 # We default rfile to buffered because otherwise it could be |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
543 # really slow for large data (a getc() call per byte); we make |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
544 # wfile unbuffered because (a) often after a write() we want to |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
545 # read and we need to flush the line; (b) big writes to unbuffered |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
546 # files are typically optimized by stdio even when big reads |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
547 # aren't. |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
548 rbufsize = -1 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
549 wbufsize = 0 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
550 |
| 2887 | 551 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
|
552 self.connection = self.request |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
553 self.rfile = self.connection.makefile('rb', self.rbufsize) |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
554 self.wfile = self.connection.makefile('wb', self.wbufsize) |
| 2887 | 555 |
| 556 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
|
557 self.wfile.flush() |
|
7840
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
558 self.wfile.close() |
|
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
559 self.rfile.close() |
| 2887 | 560 |
| 561 | |
| 562 class DatagramRequestHandler(BaseRequestHandler): | |
| 563 | |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
564 # XXX Regrettably, I cannot get this working on Linux; |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
565 # s.recvfrom() doesn't return a meaningful client address. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
566 |
| 2887 | 567 """Define self.rfile and self.wfile for datagram sockets.""" |
| 568 | |
| 569 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
|
570 import StringIO |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
571 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
|
572 self.rfile = StringIO.StringIO(self.packet) |
|
23035
b1ee4321a50a
SF bug #543318 (Frank J. Tobin).
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
573 self.wfile = StringIO.StringIO() |
| 2887 | 574 |
| 575 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
|
576 self.socket.sendto(self.wfile.getvalue(), self.client_address) |
