2017-02-15 20:07:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2017-12-13 20:14:33 +00:00
|
|
|
# Copyright 2012 - 2017, New York University and the TUF contributors
|
|
|
|
|
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2017-02-15 20:07:36 +00:00
|
|
|
"""
|
|
|
|
|
<Program>
|
|
|
|
|
simple_server.py
|
2017-12-11 20:09:26 +00:00
|
|
|
|
2017-02-15 20:07:36 +00:00
|
|
|
<Author>
|
|
|
|
|
Konstantin Andrianov.
|
|
|
|
|
|
|
|
|
|
<Started>
|
|
|
|
|
February 15, 2012.
|
2017-12-11 20:09:26 +00:00
|
|
|
|
2017-02-15 20:07:36 +00:00
|
|
|
<Copyright>
|
2018-02-05 16:31:19 +00:00
|
|
|
See LICENSE-MIT OR LICENSE for licensing information.
|
2017-02-15 20:07:36 +00:00
|
|
|
|
|
|
|
|
<Purpose>
|
2017-12-11 20:09:26 +00:00
|
|
|
This is a basic server that was designed to be used in conjunction with
|
|
|
|
|
test_download.py to test download.py module.
|
2017-02-15 20:07:36 +00:00
|
|
|
|
|
|
|
|
<Reference>
|
|
|
|
|
SimpleHTTPServer:
|
2017-12-13 18:53:24 +00:00
|
|
|
https://docs.python.org/2/library/simplehttpserver.html
|
2017-02-15 20:07:36 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Help with Python 3 compatibility, where the print statement is a function, an
|
|
|
|
|
# implicit relative import is invalid, and the '/' operator performs true
|
|
|
|
|
# division. Example: print 'hello world' raises a 'SyntaxError' exception.
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
|
|
|
|
|
PORT = 0
|
|
|
|
|
|
|
|
|
|
def _port_gen():
|
2017-12-13 20:03:55 +00:00
|
|
|
return random.SystemRandom().randint(30000, 45000)
|
2017-02-15 20:07:36 +00:00
|
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
try:
|
|
|
|
|
PORT = int(sys.argv[1])
|
2017-12-13 20:14:33 +00:00
|
|
|
|
2017-12-13 20:03:55 +00:00
|
|
|
# Enforce arbitrarily chosen port range.
|
2017-02-15 20:07:36 +00:00
|
|
|
if PORT < 30000 or PORT > 45000:
|
|
|
|
|
raise ValueError
|
2017-12-11 20:09:26 +00:00
|
|
|
|
2017-02-15 20:07:36 +00:00
|
|
|
except ValueError:
|
|
|
|
|
PORT = _port_gen()
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
PORT = _port_gen()
|
|
|
|
|
|
|
|
|
|
|
2017-12-13 20:14:33 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
Handler = six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler
|
|
|
|
|
httpd = six.moves.socketserver.TCPServer(('', PORT), Handler)
|
|
|
|
|
|
|
|
|
|
httpd.serve_forever()
|