Mercurial > cpython-lru_cache-weakref
annotate Lib/socketserver.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 7769c4d72806 |
| children |
| 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 | |
|
34939
a1293ee6e1db
Fixed a typo in docstring I happened upon.
Guido van Rossum <guido@python.org>
parents:
34293
diff
changeset
|
53 using the ForkingMixIn and ThreadingMixIn mix-in classes. For |
| 2887 | 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 |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
59 in UDPServer! Setting the various member variables also changes |
|
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
60 the behavior of the underlying server mechanism. |
| 2887 | 61 |
| 62 To implement a service, you must derive a class from | |
| 63 BaseRequestHandler and redefine its handle() method. You can then run | |
| 64 various versions of the service by combining one of the server classes | |
| 65 with your request handler class. | |
| 66 | |
| 67 The request handler class must be different for datagram or stream | |
|
35256
92b37c71f935
bug [ 957505 ] SocketServer module documentation misleading
Georg Brandl <georg@python.org>
parents:
34939
diff
changeset
|
68 services. This can be hidden by using the request handler |
|
92b37c71f935
bug [ 957505 ] SocketServer module documentation misleading
Georg Brandl <georg@python.org>
parents:
34939
diff
changeset
|
69 subclasses StreamRequestHandler or DatagramRequestHandler. |
| 2887 | 70 |
| 71 Of course, you still have to use your head! | |
| 72 | |
| 73 For instance, it makes no sense to use a forking server if the service | |
| 74 contains state in memory that can be modified by requests (since the | |
| 75 modifications in the child process would never reach the initial state | |
| 76 kept in the parent process and passed to each child). In this case, | |
| 77 you can use a threading server, but you will probably have to use | |
| 78 locks to avoid two requests that come in nearly simultaneous to apply | |
| 79 conflicting changes to the server state. | |
| 80 | |
| 81 On the other hand, if you are building e.g. an HTTP server, where all | |
| 82 data is stored externally (e.g. in the file system), a synchronous | |
| 83 class will essentially render the service "deaf" while one request is | |
| 84 being handled -- which may be for a very long time if a client is slow | |
| 73191 | 85 to read all the data it has requested. Here a threading or forking |
| 2887 | 86 server is appropriate. |
| 87 | |
| 88 In some cases, it may be appropriate to process part of a request | |
| 89 synchronously, but to finish processing in a forked child depending on | |
| 90 the request data. This can be implemented by using a synchronous | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
91 server and doing an explicit fork in the request handler class |
| 2887 | 92 handle() method. |
| 93 | |
| 94 Another approach to handling multiple simultaneous requests in an | |
| 95 environment that supports neither threads nor fork (or where these are | |
| 96 too expensive or inappropriate for the service) is to maintain an | |
| 97 explicit table of partially finished requests and to use select() to | |
| 98 decide which request to work on next (or whether to handle a new | |
| 99 incoming request). This is particularly important for stream services | |
| 100 where each client can potentially be connected for a long time (if | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
101 threads or subprocesses cannot be used). |
| 2887 | 102 |
| 103 Future work: | |
| 104 - Standard classes for Sun RPC (which uses either UDP or TCP) | |
| 105 - Standard mix-in classes to implement various authentication | |
| 106 and encryption schemes | |
| 107 - Standard framework for select-based multiplexing | |
| 108 | |
| 109 XXX Open problems: | |
| 110 - What to do with out-of-band data? | |
| 111 | |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
112 BaseServer: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
113 - 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
|
114 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
|
115 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 |
| 2887 | 120 """ |
| 121 | |
|
16037
ba295e2c73ea
Bump __version__; add authorship note for the BaseServer patch.
Guido van Rossum <guido@python.org>
parents:
16010
diff
changeset
|
122 # Author of the BaseServer patch: Luke Kenneth Casson Leighton |
| 2887 | 123 |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
124 # XXX Warning! |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
125 # 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
|
126 # standard regression test. |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
127 # 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
|
128 |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
129 __version__ = "0.4" |
| 2887 | 130 |
| 131 | |
| 132 import socket | |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
133 import select |
| 2887 | 134 import sys |
| 135 import os | |
|
76165
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
136 import errno |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
137 try: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
138 import threading |
|
84442
7769c4d72806
Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
139 except ImportError: |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
140 import dummy_threading as threading |
| 2887 | 141 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
142 __all__ = ["TCPServer","UDPServer","ForkingUDPServer","ForkingTCPServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
143 "ThreadingUDPServer","ThreadingTCPServer","BaseRequestHandler", |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
144 "StreamRequestHandler","DatagramRequestHandler", |
|
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
145 "ThreadingMixIn", "ForkingMixIn"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
146 if hasattr(socket, "AF_UNIX"): |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
147 __all__.extend(["UnixStreamServer","UnixDatagramServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
148 "ThreadingUnixStreamServer", |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
16037
diff
changeset
|
149 "ThreadingUnixDatagramServer"]) |
| 2887 | 150 |
|
76165
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
151 def _eintr_retry(func, *args): |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
152 """restart a system call interrupted by EINTR""" |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
153 while True: |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
154 try: |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
155 return func(*args) |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
156 except OSError as e: |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
157 if e.errno != errno.EINTR: |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
158 raise |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
159 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
160 class BaseServer: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
161 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
162 """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
|
163 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
164 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
|
165 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
166 - __init__(server_address, RequestHandlerClass) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
167 - serve_forever(poll_interval=0.5) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
168 - shutdown() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
169 - 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
|
170 - 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
|
171 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
172 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
|
173 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
174 - server_bind() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
175 - server_activate() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
176 - get_request() -> request, client_address |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
177 - handle_timeout() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
178 - 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
|
179 - server_close() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
180 - process_request(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
181 - shutdown_request(request) |
|
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
|
182 - close_request(request) |
|
70377
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
183 - service_actions() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
184 - handle_error() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
185 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
186 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
|
187 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
188 - 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
|
189 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
190 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
|
191 instances: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
192 |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
193 - timeout |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
194 - address_family |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
195 - socket_type |
|
30664
298888e395ce
TCPServer: Fixed typo in class docstring.
Barry Warsaw <barry@python.org>
parents:
30663
diff
changeset
|
196 - allow_reuse_address |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
197 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
198 Instance variables: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
199 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
200 - RequestHandlerClass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
201 - socket |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
202 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
203 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
204 |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
205 timeout = None |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
206 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
207 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
|
208 """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
|
209 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
|
210 self.RequestHandlerClass = RequestHandlerClass |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
211 self.__is_shut_down = threading.Event() |
|
60972
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
212 self.__shutdown_request = False |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
213 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
214 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
|
215 """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
|
216 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
217 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
218 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
219 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
220 pass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
221 |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
222 def serve_forever(self, poll_interval=0.5): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
223 """Handle one request at a time until shutdown. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
224 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
225 Polls for shutdown every poll_interval seconds. Ignores |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
226 self.timeout. If you need to do periodic tasks, do them in |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
227 another thread. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
228 """ |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
229 self.__is_shut_down.clear() |
|
60972
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
230 try: |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
231 while not self.__shutdown_request: |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
232 # XXX: Consider using another file descriptor or |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
233 # connecting to the socket to wake this up instead of |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
234 # polling. Polling reduces our responsiveness to a |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
235 # shutdown request and wastes cpu at all other times. |
|
76165
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
236 r, w, e = _eintr_retry(select.select, [self], [], [], |
|
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
237 poll_interval) |
|
60972
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
238 if self in r: |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
239 self._handle_request_noblock() |
|
70377
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
240 |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
241 self.service_actions() |
|
60972
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
242 finally: |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
243 self.__shutdown_request = False |
|
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
244 self.__is_shut_down.set() |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
245 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
246 def shutdown(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
247 """Stops the serve_forever loop. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
248 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
249 Blocks until the loop has finished. This must be called while |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
250 serve_forever() is running in another thread, or it will |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
251 deadlock. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
252 """ |
|
60972
9165232e6d1a
Merged revisions 80487,80489 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51869
diff
changeset
|
253 self.__shutdown_request = True |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
254 self.__is_shut_down.wait() |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
255 |
|
70377
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
256 def service_actions(self): |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
257 """Called by the serve_forever() loop. |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
258 |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
259 May be overridden by a subclass / Mixin to implement any code that |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
260 needs to be run during the loop. |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
261 """ |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
262 pass |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
263 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
264 # 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
|
265 # 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
|
266 # |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
267 # - handle_request() is the top-level call. It calls |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
268 # select, get_request(), verify_request() and process_request() |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
269 # - get_request() is different for stream or datagram sockets |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
270 # - 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
|
271 # 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
|
272 # - 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
|
273 # 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
|
274 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
275 def handle_request(self): |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
276 """Handle one request, possibly blocking. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
277 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
278 Respects self.timeout. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
279 """ |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
280 # Support people who used socket.settimeout() to escape |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
281 # handle_request before self.timeout was available. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
282 timeout = self.socket.gettimeout() |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
283 if timeout is None: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
284 timeout = self.timeout |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
285 elif self.timeout is not None: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
286 timeout = min(timeout, self.timeout) |
|
76165
5493d44b56d8
Issue #7978: socketserver now restarts the select() call when EINTR is returned.
Antoine Pitrou <solipsis@pitrou.net>
parents:
73348
diff
changeset
|
287 fd_sets = _eintr_retry(select.select, [self], [], [], timeout) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
288 if not fd_sets[0]: |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
289 self.handle_timeout() |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
290 return |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
291 self._handle_request_noblock() |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
292 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
293 def _handle_request_noblock(self): |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
294 """Handle one request, without blocking. |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
295 |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
296 I assume that select.select has returned that the socket is |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
297 readable before this function was called, so there should be |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
298 no risk of blocking in get_request(). |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
299 """ |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
300 try: |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
301 request, client_address = self.get_request() |
|
80933
50d19c2fac82
Issue #16717: get rid of socket.error, replace with OSError
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80932
diff
changeset
|
302 except OSError: |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
303 return |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
304 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
|
305 try: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
306 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
|
307 except: |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
308 self.handle_error(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
309 self.shutdown_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
310 |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
311 def handle_timeout(self): |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
312 """Called if no new request arrives within self.timeout. |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
313 |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
314 Overridden by ForkingMixIn. |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
315 """ |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
316 pass |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
317 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
318 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
|
319 """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
|
320 |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20636
diff
changeset
|
321 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
|
322 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
323 """ |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20636
diff
changeset
|
324 return True |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
325 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
326 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
|
327 """Call finish_request. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
328 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
329 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
|
330 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
331 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
332 self.finish_request(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
333 self.shutdown_request(request) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
334 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
335 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
|
336 """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
|
337 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
338 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
339 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
340 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
341 pass |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
342 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
343 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
|
344 """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
|
345 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
|
346 |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
347 def shutdown_request(self, request): |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
348 """Called to shutdown and close an individual request.""" |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
349 self.close_request(request) |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
350 |
|
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
|
351 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
|
352 """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
|
353 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
|
354 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
355 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
|
356 """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
|
357 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
358 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
|
359 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
360 """ |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
35256
diff
changeset
|
361 print('-'*40) |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
35256
diff
changeset
|
362 print('Exception happened during processing of request from', end=' ') |
|
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
35256
diff
changeset
|
363 print(client_address) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
364 import traceback |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
365 traceback.print_exc() # XXX But this goes to stderr! |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
35256
diff
changeset
|
366 print('-'*40) |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
367 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
368 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
369 class TCPServer(BaseServer): |
| 2887 | 370 |
| 371 """Base class for various socket-based server classes. | |
| 372 | |
| 373 Defaults to synchronous IP stream (i.e., TCP). | |
| 374 | |
| 375 Methods for the caller: | |
| 376 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
377 - __init__(server_address, RequestHandlerClass, bind_and_activate=True) |
|
45773
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
378 - serve_forever(poll_interval=0.5) |
|
832c820e9d14
Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45555
diff
changeset
|
379 - shutdown() |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
380 - 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
|
381 - fileno() -> int # for select() |
| 2887 | 382 |
| 383 Methods that may be overridden: | |
| 384 | |
| 385 - server_bind() | |
| 386 - server_activate() | |
| 387 - get_request() -> request, client_address | |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
388 - handle_timeout() |
| 2887 | 389 - verify_request(request, client_address) |
| 390 - process_request(request, client_address) | |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
391 - shutdown_request(request) |
|
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
|
392 - close_request(request) |
| 2887 | 393 - handle_error() |
| 394 | |
| 395 Methods for derived classes: | |
| 396 | |
| 397 - finish_request(request, client_address) | |
| 398 | |
| 399 Class variables that may be overridden by derived classes or | |
| 400 instances: | |
| 401 | |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
402 - timeout |
| 2887 | 403 - address_family |
| 404 - socket_type | |
| 405 - request_queue_size (only for stream sockets) | |
|
30663
5c0174e2e14f
TCPServer: Fixed typo in class docstring.
Barry Warsaw <barry@python.org>
parents:
27010
diff
changeset
|
406 - allow_reuse_address |
| 2887 | 407 |
| 408 Instance variables: | |
| 409 | |
| 410 - server_address | |
| 411 - RequestHandlerClass | |
| 412 - socket | |
| 413 | |
| 414 """ | |
| 415 | |
| 416 address_family = socket.AF_INET | |
| 417 | |
| 418 socket_type = socket.SOCK_STREAM | |
| 419 | |
| 420 request_queue_size = 5 | |
| 421 | |
|
25023
a6de50c0d457
Replace 0 with False to match working in documentation. SF 599681.
Raymond Hettinger <python@rcn.com>
parents:
23035
diff
changeset
|
422 allow_reuse_address = False |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
423 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
424 def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
425 """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
|
426 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
|
427 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
|
428 self.socket_type) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
429 if bind_and_activate: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
430 self.server_bind() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
431 self.server_activate() |
| 2887 | 432 |
| 433 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
|
434 """Called by constructor to bind the socket. |
| 2887 | 435 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
436 May be overridden. |
| 2887 | 437 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
438 """ |
|
12633
a56707b07b81
Add class variable allow_reuse_address in TCPServer -- if nonzero, the
Guido van Rossum <guido@python.org>
parents:
11382
diff
changeset
|
439 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
|
440 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
|
441 self.socket.bind(self.server_address) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40775
diff
changeset
|
442 self.server_address = self.socket.getsockname() |
| 2887 | 443 |
| 444 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
|
445 """Called by constructor to activate the server. |
| 2887 | 446 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
447 May be overridden. |
| 2887 | 448 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
449 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
450 self.socket.listen(self.request_queue_size) |
| 2887 | 451 |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
452 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
|
453 """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
|
454 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
455 May be overridden. |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
456 |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
457 """ |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
458 self.socket.close() |
|
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
459 |
| 2887 | 460 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
|
461 """Return socket file number. |
| 2887 | 462 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
463 Interface required by select(). |
| 2887 | 464 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
465 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
466 return self.socket.fileno() |
| 2887 | 467 |
| 468 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
|
469 """Get the request and client address from the socket. |
| 2887 | 470 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
471 May be overridden. |
| 2887 | 472 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
473 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
474 return self.socket.accept() |
| 2887 | 475 |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
476 def shutdown_request(self, request): |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
477 """Called to shutdown and close an individual request.""" |
|
55110
c233615b34aa
http://bugs.python.org/issue6381
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55109
diff
changeset
|
478 try: |
|
c233615b34aa
http://bugs.python.org/issue6381
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55109
diff
changeset
|
479 #explicitly shutdown. socket.close() merely releases |
|
c233615b34aa
http://bugs.python.org/issue6381
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55109
diff
changeset
|
480 #the socket and waits for GC to perform the actual close. |
|
c233615b34aa
http://bugs.python.org/issue6381
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55109
diff
changeset
|
481 request.shutdown(socket.SHUT_WR) |
|
80933
50d19c2fac82
Issue #16717: get rid of socket.error, replace with OSError
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
80932
diff
changeset
|
482 except OSError: |
|
55110
c233615b34aa
http://bugs.python.org/issue6381
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55109
diff
changeset
|
483 pass #some platforms may raise ENOTCONN here |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
484 self.close_request(request) |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
485 |
|
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
|
486 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
|
487 """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
|
488 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
|
489 |
| 2887 | 490 |
| 491 class UDPServer(TCPServer): | |
| 492 | |
| 493 """UDP server class.""" | |
| 494 | |
|
25023
a6de50c0d457
Replace 0 with False to match working in documentation. SF 599681.
Raymond Hettinger <python@rcn.com>
parents:
23035
diff
changeset
|
495 allow_reuse_address = False |
|
16010
33f5ee6a0ebe
SF Patch #102980, by Luke Kenneth Casson Leighton: BaseServer class
Guido van Rossum <guido@python.org>
parents:
15556
diff
changeset
|
496 |
| 2887 | 497 socket_type = socket.SOCK_DGRAM |
| 498 | |
| 499 max_packet_size = 8192 | |
| 500 | |
| 501 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
|
502 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
|
503 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
|
504 |
|
de0f0c3ed424
Fixed the UDP server -- this never worked. Ray Loyzaga deserves
Guido van Rossum <guido@python.org>
parents:
7840
diff
changeset
|
505 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
|
506 # 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
|
507 pass |
| 2887 | 508 |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
509 def shutdown_request(self, request): |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
510 # No need to shutdown anything. |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
511 self.close_request(request) |
|
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
512 |
|
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
|
513 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
|
514 # 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
|
515 pass |
| 2887 | 516 |
| 517 class ForkingMixIn: | |
| 518 | |
| 519 """Mix-in class to handle each request in a new process.""" | |
| 520 | |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
521 timeout = 300 |
| 2887 | 522 active_children = None |
|
11145
d997e27e3312
Another patch from Andy Dustman:
Guido van Rossum <guido@python.org>
parents:
11001
diff
changeset
|
523 max_children = 40 |
| 2887 | 524 |
| 525 def collect_children(self): | |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
526 """Internal routine to wait for children that have exited.""" |
|
45555
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
527 if self.active_children is None: return |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
528 while len(self.active_children) >= self.max_children: |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
529 # XXX: This will wait for any child process, not just ones |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
530 # spawned by this library. This could confuse other |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
531 # libraries that expect to be able to wait for their own |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
532 # children. |
|
11001
926cb5a5ee30
In collect_children(), put a try-except around os.waitpid() because it
Guido van Rossum <guido@python.org>
parents:
10992
diff
changeset
|
533 try: |
|
51869
323a5a8d10fc
Merged revisions 69990 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49239
diff
changeset
|
534 pid, status = os.waitpid(0, 0) |
|
80932
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
79394
diff
changeset
|
535 except OSError: |
|
11001
926cb5a5ee30
In collect_children(), put a try-except around os.waitpid() because it
Guido van Rossum <guido@python.org>
parents:
10992
diff
changeset
|
536 pid = None |
|
45555
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
537 if pid not in self.active_children: continue |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
538 self.active_children.remove(pid) |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
539 |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
540 # XXX: This loop runs more system calls than it ought |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
541 # to. There should be a way to put the active_children into a |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
542 # process group and then use os.waitpid(-pgid) to wait for any |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
543 # of that set, but I couldn't find a way to allocate pgids |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
544 # that couldn't collide. |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
545 for child in self.active_children: |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
546 try: |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
547 pid, status = os.waitpid(child, os.WNOHANG) |
|
80932
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
79394
diff
changeset
|
548 except OSError: |
|
45555
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
549 pid = None |
|
0cb481956452
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45371
diff
changeset
|
550 if not pid: continue |
|
45371
7c4c46342137
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
551 try: |
|
7c4c46342137
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
552 self.active_children.remove(pid) |
|
7c4c46342137
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
553 except ValueError as e: |
|
7c4c46342137
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
554 raise ValueError('%s. x=%d and list=%r' % (e.message, pid, |
|
7c4c46342137
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
555 self.active_children)) |
| 2887 | 556 |
|
44782
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
557 def handle_timeout(self): |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
558 """Wait for zombies after self.timeout seconds of inactivity. |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
559 |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
560 May be extended, do not override. |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
561 """ |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
562 self.collect_children() |
|
ca2683a67943
Merged revisions 60080-60089,60091-60093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
42429
diff
changeset
|
563 |
|
70377
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
564 def service_actions(self): |
|
79394
991c24b8969d
#11109: clean up docs, add whatsnew entry, and fix Justin's last name.
R David Murray <rdmurray@bitdance.com>
parents:
76172
diff
changeset
|
565 """Collect the zombie child processes regularly in the ForkingMixIn. |
|
70377
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
566 |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
567 service_actions is called in the BaseServer's serve_forver loop. |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
568 """ |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
569 self.collect_children() |
|
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
570 |
| 2887 | 571 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
|
572 """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
|
573 pid = os.fork() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
574 if pid: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
575 # Parent process |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
576 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
|
577 self.active_children = [] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
578 self.active_children.append(pid) |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
579 self.close_request(request) |
|
70377
e2363b1c4bca
Fix closes issue #11109 - socketserver.ForkingMixIn leaves zombies, also fails to reap all zombies in one pass.
Senthil Kumaran <senthil@uthcode.com>
parents:
70344
diff
changeset
|
580 return |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
581 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
582 # Child process. |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
583 # 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
|
584 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
585 self.finish_request(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
586 self.shutdown_request(request) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
587 os._exit(0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
588 except: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
589 try: |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
590 self.handle_error(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
591 self.shutdown_request(request) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
592 finally: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
593 os._exit(1) |
| 2887 | 594 |
| 595 | |
| 596 class ThreadingMixIn: | |
| 597 """Mix-in class to handle each request in a new thread.""" | |
| 598 | |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
599 # Decides how threads will act upon termination of the |
|
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
600 # main process |
| 26347 | 601 daemon_threads = False |
|
26337
733e4b9d592e
Patch #550765: Add daemon_threads flag.
Martin v. Löwis <martin@v.loewis.de>
parents:
25023
diff
changeset
|
602 |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
603 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
|
604 """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
|
605 |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
606 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
|
607 |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
608 """ |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
609 try: |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
610 self.finish_request(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
611 self.shutdown_request(request) |
|
20636
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
612 except: |
|
3f80c91b5c3c
Apply the first chunk of the second patch from SF bug #471720:
Guido van Rossum <guido@python.org>
parents:
20520
diff
changeset
|
613 self.handle_error(request, client_address) |
|
55140
81002c7600bd
http://bugs.python.org/issue6382
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55110
diff
changeset
|
614 self.shutdown_request(request) |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
615 |
| 2887 | 616 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
|
617 """Start a new thread to process the request.""" |
|
20520
bc3c8419874c
SF bug #471720: ThreadingMixIn/TCPServer forgets close
Guido van Rossum <guido@python.org>
parents:
20230
diff
changeset
|
618 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
|
619 args = (request, client_address)) |
|
73348
94017ce9304d
Closes #13140: Fix the daemon_threads attribute of ThreadingMixIn.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
73191
diff
changeset
|
620 t.daemon = self.daemon_threads |
|
11382
1ef1807377bf
update to use threading module instead of thread.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11145
diff
changeset
|
621 t.start() |
| 2887 | 622 |
| 623 | |
| 624 class ForkingUDPServer(ForkingMixIn, UDPServer): pass | |
| 625 class ForkingTCPServer(ForkingMixIn, TCPServer): pass | |
| 626 | |
| 627 class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass | |
| 628 class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass | |
| 629 | |
|
9544
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
630 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
|
631 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
632 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
|
633 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
|
634 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
635 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
|
636 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
|
637 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
638 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
|
639 |
|
16af2582c964
Patch by Jeff Bauer: a minor change to declare two new
Guido van Rossum <guido@python.org>
parents:
9485
diff
changeset
|
640 class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): pass |
| 2887 | 641 |
| 642 class BaseRequestHandler: | |
| 643 | |
| 644 """Base class for request handler classes. | |
| 645 | |
| 646 This class is instantiated for each request to be handled. The | |
| 647 constructor sets the instance variables request, client_address | |
| 648 and server, and then calls the handle() method. To implement a | |
| 649 specific service, all you need to do is to derive a class which | |
| 650 defines a handle() method. | |
| 651 | |
| 652 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
|
653 client address as self.client_address, and the server (in case it |
| 2887 | 654 needs access to per-server information) as self.server. Since a |
| 655 separate instance is created for each request, the handle() method | |
| 656 can define arbitrary other instance variariables. | |
| 657 | |
| 658 """ | |
| 659 | |
| 660 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
|
661 self.request = request |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
662 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
|
663 self.server = server |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41760
diff
changeset
|
664 self.setup() |
|
55028
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
665 try: |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
666 self.handle() |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
667 finally: |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
668 self.finish() |
| 2887 | 669 |
| 670 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
|
671 pass |
| 2887 | 672 |
| 673 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
|
674 pass |
| 2887 | 675 |
| 676 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
|
677 pass |
| 2887 | 678 |
| 679 | |
| 680 # The following two classes make it possible to use the same service | |
| 681 # class for stream or datagram servers. | |
| 682 # Each class sets up these instance variables: | |
| 683 # - rfile: a file object from which receives the request is read | |
| 684 # - wfile: a file object to which the reply is written | |
| 685 # When the handle() method returns, wfile is flushed properly | |
| 686 | |
| 687 | |
| 688 class StreamRequestHandler(BaseRequestHandler): | |
| 689 | |
| 690 """Define self.rfile and self.wfile for stream sockets.""" | |
| 691 | |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
692 # 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
|
693 # 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
|
694 # 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
|
695 # 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
|
696 # 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
|
697 # 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
|
698 # aren't. |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
699 rbufsize = -1 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
700 wbufsize = 0 |
|
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
701 |
|
55028
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
702 # A timeout to apply to the request socket, if not None. |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
703 timeout = None |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
704 |
|
68489
81d7f78a633a
#11515: Merge with 3.1.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
68257
diff
changeset
|
705 # Disable nagle algorithm for this socket, if True. |
|
54938
52840be42530
http://bugs.python.org/issue6192
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51869
diff
changeset
|
706 # Use only when wbufsize != 0, to avoid small packets. |
|
52840be42530
http://bugs.python.org/issue6192
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51869
diff
changeset
|
707 disable_nagle_algorithm = False |
|
52840be42530
http://bugs.python.org/issue6192
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51869
diff
changeset
|
708 |
| 2887 | 709 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
|
710 self.connection = self.request |
|
55028
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
711 if self.timeout is not None: |
|
366c8a4e5a46
http://bugs.python.org/issue6267
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
54938
diff
changeset
|
712 self.connection.settimeout(self.timeout) |
|
54938
52840be42530
http://bugs.python.org/issue6192
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51869
diff
changeset
|
713 if self.disable_nagle_algorithm: |
|
52840be42530
http://bugs.python.org/issue6192
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51869
diff
changeset
|
714 self.connection.setsockopt(socket.IPPROTO_TCP, |
|
52840be42530
http://bugs.python.org/issue6192
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
51869
diff
changeset
|
715 socket.TCP_NODELAY, True) |
|
14331
cbe423b3ba3a
In class StreamRequestHandler, make the default buffering for rfile
Guido van Rossum <guido@python.org>
parents:
12633
diff
changeset
|
716 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
|
717 self.wfile = self.connection.makefile('wb', self.wbufsize) |
| 2887 | 718 |
| 719 def finish(self): | |
|
27010
2687a9085411
Clearing out old patch queue. Patch #558547, make SocketServer more
Anthony Baxter <anthonybaxter@gmail.com>
parents:
26347
diff
changeset
|
720 if not self.wfile.closed: |
|
81066
7e5d7ef4634d
Issue #14574: Ignore socket errors raised when flushing a connection on close.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
76171
diff
changeset
|
721 try: |
|
7e5d7ef4634d
Issue #14574: Ignore socket errors raised when flushing a connection on close.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
76171
diff
changeset
|
722 self.wfile.flush() |
|
7e5d7ef4634d
Issue #14574: Ignore socket errors raised when flushing a connection on close.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
76171
diff
changeset
|
723 except socket.error: |
|
7e5d7ef4634d
Issue #14574: Ignore socket errors raised when flushing a connection on close.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
76171
diff
changeset
|
724 # An final socket error may have occurred here, such as |
|
7e5d7ef4634d
Issue #14574: Ignore socket errors raised when flushing a connection on close.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
76171
diff
changeset
|
725 # the local error ECONNABORTED. |
|
7e5d7ef4634d
Issue #14574: Ignore socket errors raised when flushing a connection on close.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
76171
diff
changeset
|
726 pass |
|
7840
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
727 self.wfile.close() |
|
aaa204a05f84
Explicitly close rfile and wfile in StreamRequestHandler.finish() --
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
728 self.rfile.close() |
| 2887 | 729 |
| 730 | |
| 731 class DatagramRequestHandler(BaseRequestHandler): | |
| 732 | |
|
18471
99ad6fd279e9
Backport of Guido's checkin 1.25:
Thomas Wouters <thomas@python.org>
parents:
17400
diff
changeset
|
733 # 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
|
734 # 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
|
735 |
| 2887 | 736 """Define self.rfile and self.wfile for datagram sockets.""" |
| 737 | |
| 738 def setup(self): | |
|
42429
b1bf1bab9779
SF patch# 1764815 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents:
41969
diff
changeset
|
739 from io import BytesIO |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6346
diff
changeset
|
740 self.packet, self.socket = self.request |
|
42429
b1bf1bab9779
SF patch# 1764815 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents:
41969
diff
changeset
|
741 self.rfile = BytesIO(self.packet) |
|
b1bf1bab9779
SF patch# 1764815 by Paul Colomiets.
Guido van Rossum <guido@python.org>
parents:
41969
diff
changeset
|
742 self.wfile = BytesIO() |
| 2887 | 743 |
| 744 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
|
745 self.socket.sendto(self.wfile.getvalue(), self.client_address) |
