Skip to content

Commit

Permalink
server.py: Updated to latest ssl API. Added handler for the src dir
Browse files Browse the repository at this point in the history
Flag global USE_HTTPS to optionally serve through HTTP.
Handler: overrides SimpleHTTPRequestHandler.translate_path to
	 accomodate /src URLs.
  • Loading branch information
pupitetris committed Jun 20, 2023
1 parent 5af8a08 commit dbbee02
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl

#USE_HTTPS = False
USE_HTTPS = True

if USE_HTTPS:
PORT = 4443
# https://wiki.debian.org/Self-Signed_Certificate
CERTFILE = './star.pem' # copy from /etc/ssl/certs/ssl-cert-snakeoil.pem for example.
KEYFILE = './star.key' # copy from /etc/ssl/private/ssl-cert-snakeoil.key
else:
PORT=8002

class Handler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory='htdocs', **kwargs)

def translate_path(self, path):
if self.path.startswith('/src/'):
return '.' + path;
return super().translate_path(path);

print("Configuring Server")
httpd = HTTPServer(('0.0.0.0', 4443), SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile="./star.pem",
certfile='./star.pem', server_side=True)
httpd = HTTPServer(('0.0.0.0', PORT), Handler)
if USE_HTTPS:
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(certfile=CERTFILE, keyfile=KEYFILE)
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)

print("Starting Server")
print("Starting server at port " + str(PORT))
httpd.serve_forever()
print("Done")

0 comments on commit dbbee02

Please sign in to comment.