Gangmax Blog

Simple HTTPS Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
#
# Generate server.pem with the following command:
#
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
#
# Put the "server.pem" file with this "simple-https-server.py" file, and execute:
#
# python simple-https-server.py
#
# Then in your browser, visit:
#
# https://localhost:4443
#
# To make this HTTPS can be accessed by Java, we need to import the certificate
# into JDK.
#
# From: https://www.grim.se/guide/jre-cert

from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
import ssl

httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
certfile='server.pem', server_side=True)

httpd.serve_forever()

Comments