Python HTTPS Simple Server
· Howto · Python · Networking
When you need to quickly share files or test a web app, Python has a built-in answer, nothing extra to install. Many times Netcat1 is enough for a single-use transfer, but when sharing multiple files or even a website, a proper HTTP server is more practical.
Python’s HTTPServer, or SimpleHTTPServer in Python 2, is a quick way to start a local HTTP server on your network.
NOTES
http.server is not recommended for production.
It only implements basic security checks.
ssl.wrap_socket was deprecated in Python 3.7 and removed in Python 3.12.
The Python 3 example below wraps the socket through an ssl.SSLContext instead.
The Python 2 examples still call the removed function,
so they will not run on a current interpreter.
Python 2 is deprecated and should be avoided if possible. Yet Python 2 Examples are still available below for legacy reference.
HTTP Web Server
The module can be easily invoked,
from the directory we want to share,
using the -m switch of the interpreter.
python3 -m http.server 8000
Or imported as a module, for additional configuration.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
HTTP Web Server with SSL Support
Self-signed certificates
If you are going to create a server that provides SSL-encrypted connection services, you will need a certificate. Besides buying a certificate from a certification authority, another common practice is to generate a self-signed certificate. The simplest way to do this is with the OpenSSL tool, using something like the following:
openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
HTTPS Server
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
import ssl
server_address = ("localhost", 4443)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile="cert.pem")
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()
load_cert_chain reads the private key from the certificate file
when no keyfile is given,
which is why the openssl command above writes both into cert.pem.
Python 2 (Legacy)
Python 2 reached end-of-life in January 2020. The examples below are kept for reference only.
Interpreter Module Invocation
python -m SimpleHTTPServer 8000
Imported Module - HTTP Server
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
Imported Module - HTTPS Server
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import BaseHTTPServer
import SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(("localhost", 4443),
SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
server_side=True,
certfile="cert.pem")
httpd.serve_forever()
-
Netcat file transfer:
nc -nlvp 9000 > filenc <listener ip> 9000 < file↩