2014-06-17 14:25:16 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2017-11-30 18:33:11 +00:00
|
|
|
# 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
|
2017-11-30 18:33:11 +00:00
|
|
|
|
2014-06-17 14:25:16 +00:00
|
|
|
<Author>
|
|
|
|
|
Vladimir Diaz.
|
|
|
|
|
|
|
|
|
|
<Started>
|
|
|
|
|
June 17, 2014
|
|
|
|
|
|
|
|
|
|
<Copyright>
|
2018-02-05 16:31:19 +00:00
|
|
|
See LICENSE-MIT OR LICENSE 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.
|
2017-11-30 18:33:11 +00:00
|
|
|
|
2014-06-17 14:25:16 +00:00
|
|
|
<Reference>
|
|
|
|
|
ssl.wrap_socket:
|
|
|
|
|
https://docs.python.org/2/library/ssl.html#functions-constants-and-exceptions
|
2017-11-30 18:33:11 +00:00
|
|
|
|
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 ssl
|
2018-09-11 19:30:40 +00:00
|
|
|
import os
|
2015-06-02 14:28:02 +00:00
|
|
|
import six
|
2014-06-17 14:25:16 +00:00
|
|
|
|
2018-09-21 16:08:44 +00:00
|
|
|
keyfile = os.path.join('ssl_certs', 'ssl_cert.key')
|
|
|
|
|
certfile = os.path.join('ssl_certs', 'ssl_cert.crt')
|
2018-09-11 19:30:40 +00:00
|
|
|
|
2014-06-17 14:25:16 +00:00
|
|
|
|
2020-10-27 15:28:01 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
if os.path.exists(sys.argv[1]):
|
|
|
|
|
certfile = sys.argv[1]
|
2018-09-11 19:30:40 +00:00
|
|
|
else:
|
2020-10-27 15:28:01 +00:00
|
|
|
print('simple_https_server: cert file not found: ' + sys.argv[1] +
|
2018-09-11 19:30:40 +00:00
|
|
|
'; using default: ' + certfile)
|
|
|
|
|
|
2020-10-27 15:28:01 +00:00
|
|
|
httpd = six.moves.BaseHTTPServer.HTTPServer(('localhost', 0),
|
|
|
|
|
six.moves.SimpleHTTPServer.SimpleHTTPRequestHandler)
|
2014-06-17 14:25:16 +00:00
|
|
|
|
2018-09-11 19:30:40 +00:00
|
|
|
httpd.socket = ssl.wrap_socket(
|
|
|
|
|
httpd.socket, keyfile=keyfile, certfile=certfile, server_side=True)
|
2014-06-17 14:25:16 +00:00
|
|
|
|
2020-10-27 15:28:01 +00:00
|
|
|
port_message = 'bind succeeded, server port is: ' \
|
|
|
|
|
+ str(httpd.server_address[1])
|
|
|
|
|
print(port_message)
|
2014-06-17 14:25:16 +00:00
|
|
|
httpd.serve_forever()
|