-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve_local_https.py
More file actions
executable file
·59 lines (45 loc) · 1.59 KB
/
serve_local_https.py
File metadata and controls
executable file
·59 lines (45 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import argparse
import atexit
import logging
import os
import sys
import tempfile
from subprocess import call
import BaseHTTPServer
import SimpleHTTPServer
import ssl
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(description='')
parser.add_argument('--host', dest='host', default='localhost')
parser.add_argument('--port', dest='port', type=int, default=4443)
args = parser.parse_args()
server_host = args.host
server_port = args.port
ssl_cert_path = '{}/server.pem'.format(tempfile.gettempdir())
OpenSslExecutableNotFoundError = OSError
def create_ssl_cert():
DEVNULL = open(os.devnull, 'wb')
try:
ssl_exec_list = ['openssl', 'req', '-new', '-x509', '-keyout', ssl_cert_path,
'-out', ssl_cert_path, '-days', '365', '-nodes',
'-subj', '/CN=Local Testing Cert/O=X/C=US']
call(ssl_exec_list, stdout=DEVNULL, stderr=DEVNULL)
except OpenSslExecutableNotFoundError:
logging.error('openssl executable not found!')
exit(1)
logging.info(
'Self signed ssl certificate created at {}'.format(ssl_cert_path))
def exit_handler():
# remove certificate file
os.remove(ssl_cert_path)
logging.info('Bye!')
create_ssl_cert()
atexit.register(exit_handler)
logging.info(
'Server running... https://{}:{}'.format(server_host, server_port))
httpd = BaseHTTPServer.HTTPServer(
(server_host, server_port), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(
httpd.socket, certfile=ssl_cert_path, server_side=True)
httpd.serve_forever()