radames commited on
Commit
fca36d1
1 Parent(s): 2c0ada2

simple https local server

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. server.py +25 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ *.pem
server.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/python3
2
+ # taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
3
+ # generate server.xml with the following command:
4
+ # openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
5
+ # run as follows:
6
+ # python simple-https-server.py
7
+ # then in your browser, visit:
8
+ # https://localhost:4443
9
+ #
10
+ # openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
11
+
12
+ from http.server import HTTPServer, SimpleHTTPRequestHandler
13
+ import ssl, os
14
+
15
+ if not os.path.exists("key.pem") or not os.path.exists("cert.pem"):
16
+ os.system(
17
+ "openssl req -nodes -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj '/CN=mylocalhost'"
18
+ )
19
+ port = 443
20
+ httpd = HTTPServer(("0.0.0.0", port), SimpleHTTPRequestHandler)
21
+ httpd.socket = ssl.wrap_socket(
22
+ httpd.socket, keyfile="key.pem", certfile="cert.pem", server_side=True
23
+ )
24
+ print(f"Server running on https://0.0.0.0:{port}")
25
+ httpd.serve_forever()