Python HTTP Server

HTTP Server

python3 -m http.server <PORT>

HTTPS Server

  • Folder structure

folderX/
├── cert/
   ├── cert.pem
   └── privkey.pem
└── project/
    └── https_server.py
 
  • Create project/https_server.py:

import http.server
import ssl

PORT = 8443

httpd = http.server.HTTPServer(
    ("0.0.0.0", PORT),
    http.server.SimpleHTTPRequestHandler
)

context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile="../cert/cert.pem", keyfile="../cert/key.pem")

httpd.socket = context.wrap_socket(httpd.socket, server_side=True)

print(f"Serving HTTPS on port {PORT}")
httpd.serve_forever()
  • Create a self-signed test certificate:

  • Run the server

Last updated