A simple HTTPS server with mutual TLS authentication using Python's built-in modules.
- ✅ Mutual TLS authentication
- ✅ Client certificate verification
- ✅ RESTful API endpoints
- ✅ No external dependencies (uses stdlib only)
- ✅ Graceful shutdown
- Python 3.7+ (built-in
sslandhttp.servermodules) - Certificates generated by the mtls CLI tool
# No dependencies needed for basic functionality
# Optional: Install development tools
pip install -r requirements.txt# Make executable (optional)
chmod +x server.py
# Run directly
python3 server.py
# Or
./server.pyThe server will start on https://localhost:8443.
GET /- Main endpoint with client certificate infoGET /health- Health check endpointGET /api/data- Sample data endpointPOST /api/echo- Echo back the request body
You can test the server using:
- The Python client in
../python-client - curl with certificates:
curl --cert ../../certs/servers/localhost/server-cert.pem \
--key ../../certs/servers/localhost/server-key.pem \
--cacert ../../certs/ca/ca-cert.pem \
https://localhost:8443/The server creates an SSL context with mTLS:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.minimum_version = ssl.TLSVersion.TLSv1_2
# Load server certificate
context.load_cert_chain(
certfile='server-cert.pem',
keyfile='server-key.pem'
)
# Load CA for client verification
context.load_verify_locations(cafile='ca-cert.pem')
# Require client certificate
context.verify_mode = ssl.CERT_REQUIREDThe server extracts client certificate information:
cert = connection.getpeercert()
subject = dict(x[0] for x in cert.get('subject', []))
cn = subject.get('commonName')The server will reject connections from:
- Clients without a certificate
- Clients with invalid certificates
- Clients with certificates not signed by the trusted CA
Press Ctrl+C to gracefully shutdown the server.
To change the port, modify the port variable in server.py:
port = 8443 # Change to your desired portSSL certificate error:
- Ensure certificate paths are correct
- Check certificate permissions
- Verify certificates are not expired
Permission denied on port 443:
- Use port 8443 or higher
- Or run with sudo (not recommended)
Client verification fails:
- Check CA certificate is loaded correctly
- Ensure client certificate is signed by the same CA