Testing Certifcates using HTTP Server in Python

It can be a good and reassuring to verify certificates before it comes time to deploy them to their endpoints. For example, you can load and check the certificate for issues and correctiness. Verify in different browsers that might have different certificate chains, etc.

from http.server import HTTPServer, SimpleHTTPRequestHandler
from ssl import SSLContext, PROTOCOL_TLS_SERVER

# Create an SSL context with the specified certificate and key files
ssl_context = SSLContext(PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile='server.crt', keyfile='server.key')

httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler)
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)


You can save this to a file or paste it directly to the Python interpreter. It will attempt to load certificate and key from files server.crt and server.key respectfully.

Apr 6, 2023 · Filed in: Python, HTTPS
Words: 200