|
25 | 25 | http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
|
26 | 26 | """
|
27 | 27 |
|
28 |
| -import sys |
29 |
| -import random |
30 | 28 | import socketserver
|
| 29 | +import sys |
31 | 30 | from http.server import SimpleHTTPRequestHandler
|
| 31 | +from typing import Type, Union |
32 | 32 |
|
33 | 33 |
|
34 | 34 | class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
|
35 |
| - """A SimpleHTTPRequestHandler that does not write incoming requests to |
36 |
| - stderr. """ |
37 |
| - def log_request(self, code='-', size='-'): |
38 |
| - pass |
| 35 | + """A SimpleHTTPRequestHandler that does not write incoming requests to |
| 36 | + stderr.""" |
| 37 | + |
| 38 | + def log_request( |
| 39 | + self, code: Union[int, str] = "-", size: Union[int, str] = "-" |
| 40 | + ) -> None: |
| 41 | + pass |
| 42 | + |
39 | 43 |
|
40 | 44 | # NOTE: On Windows/Python2 tests that use this simple_server.py in a
|
41 | 45 | # subprocesses hang after a certain amount of requests (~68), if a PIPE is
|
42 | 46 | # passed as Popen's stderr argument. This problem doesn't emerge if
|
43 | 47 | # we silence the HTTP messages.
|
44 | 48 | # If you decide to receive the HTTP messages, then this bug
|
45 | 49 | # could reappear.
|
46 |
| -use_quiet_http_request_handler = True |
47 | 50 |
|
48 |
| -if len(sys.argv) > 2: |
49 |
| - use_quiet_http_request_handler = sys.argv[2] |
| 51 | +# pylint: disable=invalid-name |
| 52 | +handler: Type[ |
| 53 | + Union[SimpleHTTPRequestHandler, QuietHTTPRequestHandler] |
| 54 | +] = QuietHTTPRequestHandler |
50 | 55 |
|
51 |
| -if use_quiet_http_request_handler: |
52 |
| - handler = QuietHTTPRequestHandler |
53 |
| -else: |
54 |
| - handler = SimpleHTTPRequestHandler |
| 56 | +if len(sys.argv) > 2: |
| 57 | + handler = SimpleHTTPRequestHandler |
55 | 58 |
|
56 | 59 | # Allow re-use so you can re-run tests as often as you want even if the
|
57 | 60 | # tests re-use ports. Otherwise TCP TIME-WAIT prevents reuse for ~1 minute
|
58 | 61 | socketserver.TCPServer.allow_reuse_address = True
|
59 | 62 |
|
60 |
| -httpd = socketserver.TCPServer(('localhost', 0), handler) |
61 |
| -port_message = 'bind succeeded, server port is: ' \ |
62 |
| - + str(httpd.server_address[1]) |
| 63 | +httpd = socketserver.TCPServer(("localhost", 0), handler) |
| 64 | +port_message = "bind succeeded, server port is: " + str(httpd.server_address[1]) |
63 | 65 | print(port_message)
|
64 | 66 | httpd.serve_forever()
|
0 commit comments