|
3 | 3 | # Copyright 2012 - 2017, New York University and the TUF contributors |
4 | 4 | # SPDX-License-Identifier: MIT OR Apache-2.0 |
5 | 5 |
|
6 | | -""" |
7 | | -<Program> |
8 | | - simple_server.py |
9 | | -
|
10 | | -<Author> |
11 | | - Konstantin Andrianov. |
12 | | -
|
13 | | -<Started> |
14 | | - February 15, 2012. |
15 | | -
|
16 | | -<Copyright> |
17 | | - See LICENSE-MIT or LICENSE for licensing information. |
18 | | -
|
19 | | -<Purpose> |
20 | | - This is a basic server that was designed to be used in conjunction with |
21 | | - test_download.py to test download.py module. |
22 | | -
|
23 | | -<Reference> |
24 | | - SimpleHTTPServer: |
25 | | - http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer |
26 | | -""" |
| 6 | +"""Simple HTTP server for python-tuf tests""" |
27 | 7 |
|
28 | 8 | import socketserver |
29 | | -import sys |
30 | 9 | from http.server import SimpleHTTPRequestHandler |
31 | | -from typing import Type, Union |
32 | | - |
33 | | - |
34 | | -class QuietHTTPRequestHandler(SimpleHTTPRequestHandler): |
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 | | - |
43 | | - |
44 | | -# NOTE: On Windows/Python2 tests that use this simple_server.py in a |
45 | | -# subprocesses hang after a certain amount of requests (~68), if a PIPE is |
46 | | -# passed as Popen's stderr argument. This problem doesn't emerge if |
47 | | -# we silence the HTTP messages. |
48 | | -# If you decide to receive the HTTP messages, then this bug |
49 | | -# could reappear. |
50 | | - |
51 | | -# pylint: disable=invalid-name |
52 | | -handler: Type[Union[SimpleHTTPRequestHandler, QuietHTTPRequestHandler]] |
53 | | - |
54 | | -if len(sys.argv) > 2 and sys.argv[2]: |
55 | | - handler = QuietHTTPRequestHandler |
56 | | -else: |
57 | | - handler = SimpleHTTPRequestHandler |
58 | 10 |
|
59 | 11 | # Allow re-use so you can re-run tests as often as you want even if the |
60 | 12 | # tests re-use ports. Otherwise TCP TIME-WAIT prevents reuse for ~1 minute |
61 | 13 | socketserver.TCPServer.allow_reuse_address = True |
62 | 14 |
|
63 | | -httpd = socketserver.TCPServer(("localhost", 0), handler) |
| 15 | +httpd = socketserver.TCPServer(("localhost", 0), SimpleHTTPRequestHandler) |
64 | 16 | port_message = "bind succeeded, server port is: " + str(httpd.server_address[1]) |
65 | 17 | print(port_message) |
66 | 18 | httpd.serve_forever() |
0 commit comments