Skip to content

Instantly share code, notes, and snippets.

@Graham42
Last active January 20, 2020 03:51
Show Gist options
  • Save Graham42/b6dd41f7c5d079f484201336d4144fa6 to your computer and use it in GitHub Desktop.
Save Graham42/b6dd41f7c5d079f484201336d4144fa6 to your computer and use it in GitHub Desktop.
Serve directory with https (SSL, TLS) self-signed
FROM python:3-alpine
RUN apk --no-cache upgrade && \
apk --no-cache add \
openssl
WORKDIR /app
RUN openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes \
-subj "/C=CA/ST=Ontario/L=Toronto"
COPY serve.py index.html /app/
ENTRYPOINT ["python", "/app/serve.py"]
<h1>Hello World</h1>
#!/usr/bin/env bash
set -e
[ ! -z "$1" ] || (>&2 echo "missing target" && exit 1)
TARGET=$(realpath "$1")
docker build --pull -t serve-https .
echo Starting at https://0.0.0.0:8443 ...
docker run --rm -it -v "$TARGET":/app/web -p 8443:443 serve-https
#!/usr/bin/env python3
import http.server
import socketserver
import ssl
import os
PORT = 443
web_dir = os.path.join(os.path.dirname(__file__), 'web')
os.chdir(web_dir)
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("0.0.0.0", PORT), Handler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='/app/server.pem', server_side=True)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment