Skip to content

Instantly share code, notes, and snippets.

@aont
Created September 5, 2025 08:44
Show Gist options
  • Select an option

  • Save aont/b50f33b5453ae47722cf83c6ea656336 to your computer and use it in GitHub Desktop.

Select an option

Save aont/b50f33b5453ae47722cf83c6ea656336 to your computer and use it in GitHub Desktop.

A quick way to serve local https server

Generate self-signed cert files

openssl genpkey -algorithm RSA -out key.pem
openssl req -new -key key.pem -out cert.csr -subj "/C=JP/ST=Tokyo/L=Chiyoda/O=Example Inc./CN=localhost"
openssl x509 -req -in cert.csr -signkey key.pem -out cert.pem -days 365

install aiohttp

python -m venv venv
. ./venv/bin/activate
pip install aiohttp

start server.py

python server.py
# https_static.py
import ssl
from aiohttp import web
import argparse
import pathlib
import logging
logging.basicConfig(level=logging.INFO)
def make_app(root_dir: str):
app = web.Application()
app.router.add_static('/', path=root_dir, show_index=True)
return app
def make_ssl_context(certfile: str, keyfile: str) -> ssl.SSLContext:
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(certfile=certfile, keyfile=keyfile)
return ctx
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("--port", "-p", type=int, default=8080, help="port (default: 8080)")
p.add_argument("--root", "-r", type=str, default=".", help="directory (default: current directory)")
p.add_argument("--cert", type=str, default="cert.pem", help="cert file (PEM)")
p.add_argument("--key", type=str, default="key.pem", help="private key file (PEM)")
args = p.parse_args()
root = pathlib.Path(args.root).resolve()
if not root.exists():
raise SystemExit(f"directory not found: {root}")
app = make_app(str(root))
ssl_ctx = make_ssl_context(args.cert, args.key)
logging.info(f"Serving {root} on https://0.0.0.0:{args.port}/")
web.run_app(app, port=args.port, ssl_context=ssl_ctx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment