Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
112 lines (78 loc) · 3.04 KB
/
Copy pathserver.py
File metadata and controls
112 lines (78 loc) · 3.04 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import argparse
import atexit
import logging
import os
import sys
import inspect
import shutil
from string import Template
from subprocess import call
logging.basicConfig(level=logging.INFO)
country = "US"
state = "California"
city = "San Francisco"
organization = "foo bar Company ltd."
organization_unit = "DevOps Dep"
email = "master@foobar-company-ltd.com"
hostname = "localhost"
PY3 = sys.version_info[0] == 3
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
user_desktop_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
logging.info(user_desktop_path)
certs_path = user_desktop_path + '/certs/'
if os.path.exists(certs_path):
shutil.rmtree(certs_path)
os.makedirs(certs_path)
ssl_cert_path = certs_path + hostname + '.crt'
ssl_key_path = certs_path + hostname + '.key'
ssl_server_key_crt_path = certs_path + '/server.pem'
config_out_path = certs_path + hostname + '.cnf'
OpenSslExecutableNotFoundError = FileNotFoundError
def create_ssl_config_file() :
template_path = user_desktop_path + '/openssl-template.cnf'
filein = open( template_path )
src = Template( filein.read() )
d={ 'country':country, 'state':state, 'city':city,
'organization':organization,'organization_unit':organization_unit,
'email':email,'hostname':hostname}
result = src.substitute(d)
with open(config_out_path, 'a') as the_file:
the_file.write(result)
def create_ssl_cert():
from subprocess import DEVNULL
try:
ssl_exec_list = ['C:/OpenSSL-Win64/bin/openssl', 'req', '-new', '-x509','-newkey', 'rsa:2048', '-keyout', ssl_key_path,
'-out', ssl_cert_path, '-days', '365', '-nodes', '-config', config_out_path]
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 merge_cert_and_privatekey_to_server_pem():
filenames = [ssl_cert_path, ssl_key_path]
with open(ssl_server_key_crt_path, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
def exit_handler():
#os.remove(ssl_cert_path)
logging.info('Bye!')
def main():
logging.info('pyhttps {}')
create_ssl_config_file()
create_ssl_cert()
merge_cert_and_privatekey_to_server_pem()
atexit.register(exit_handler)
import http.server
import socketserver
import ssl
logging.info('Server running... https://{}:{}'.format(server_host, server_port))
httpd = socketserver.TCPServer((server_host, server_port), http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=ssl_server_key_crt_path, server_side=True)
httpd.serve_forever()
main()