Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rozifus
Last active October 9, 2022 22:40
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 23 You must be signed in to fork a gist
  • Save rozifus/c529caf170699f117c53 to your computer and use it in GitHub Desktop.
Save rozifus/c529caf170699f117c53 to your computer and use it in GitHub Desktop.
Python SimpleHTTPServer with SSL
#!/bin/bash
openssl req -new -x509 -keyout yourpemfile.pem -out yourpemfile.pem -days 365 -nodes
import BaseHTTPServer, SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, server_side=True,
certfile='yourpemfile.pem')
httpd.serve_forever()
# useful for running ssl server on localhost
# which in turn is useful for working with WebSocket Secure (wss)
# copied from http://www.piware.de/2011/01/creating-an-https-server-in-python/
@stucka
Copy link

stucka commented Jul 19, 2018

Python 3 version, perhaps:

from http.server import HTTPServer, SimpleHTTPRequestHandler, HTTPStatus
import ssl
# given a pem file ... openssl req -new -x509 -keyout yourpemfile.pem -out yourpemfile.pem -days 365 -nodes

httpd = HTTPServer(('localhost', 443), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, server_side=True,
                                certfile='yourpemfile.pem')
httpd.serve_forever()

@rayworks
Copy link

Once server gets started with above generated pem file, I got this error on Chrome (Version 88.0.4324.96 )
Your connection is not private Attackers might be trying to steal your information from localhost (for example, passwords, messages, or credit cards). Learn more NET::ERR_CERT_INVALID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment