python-tuf/tests/simple_https_server.py

72 lines
1.8 KiB
Python
Raw Normal View History

2014-06-17 14:25:16 +00:00
#!/usr/bin/env python
# Copyright 2014 - 2017, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
2014-06-17 14:25:16 +00:00
"""
<Program>
simple_https_server.py
2014-06-17 14:25:16 +00:00
<Author>
Vladimir Diaz.
<Started>
June 17, 2014
<Copyright>
See LICENSE-MIT.txt OR LICENSE-APACHE.txt for licensing information.
2014-06-17 14:25:16 +00:00
<Purpose>
Provide a simple https server that can be used by the unit tests. For
example, 'download.py' can connect to the https server started by this module
to verify that https downloads are permitted.
2014-06-17 14:25:16 +00:00
<Reference>
ssl.wrap_socket:
https://docs.python.org/2/library/ssl.html#functions-constants-and-exceptions
2014-06-17 14:25:16 +00:00
SimpleHTTPServer:
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
"""
# 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 ssl
import six
2014-06-17 14:25:16 +00:00
PORT = 0
def _generate_random_port():
return random.randint(30000, 45000)
if len(sys.argv) > 1:
try:
PORT = int(sys.argv[1])
if PORT < 30000 or PORT > 45000:
raise ValueError
2014-06-17 14:25:16 +00:00
except ValueError:
PORT = _generate_random_port()
else:
PORT = _generate_random_port()
httpd = six.moves.BaseHTTPServer.HTTPServer(('localhost', PORT),
six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile='ssl_cert.key',
certfile='ssl_cert.crt',
2014-06-17 14:25:16 +00:00
server_side=True)
#print('Starting https server on port: ' + str(PORT))
httpd.serve_forever()