-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_server.py
More file actions
66 lines (56 loc) · 2.12 KB
/
https_server.py
File metadata and controls
66 lines (56 loc) · 2.12 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""
Simple HTTPS server for local development.
Automatically generates a self-signed certificate if one doesn't exist,
then serves the current directory over HTTPS on port 4443.
Usage:
python3 https_server.py
"""
import http.server
import ssl
import subprocess
import os
import sys
CERT_FILE = 'cert.pem'
KEY_FILE = 'key.pem'
PORT = 4443
def generate_certificate():
"""Generate a self-signed SSL certificate for localhost."""
print("Generating self-signed certificate for localhost...")
try:
subprocess.run([
'openssl', 'req', '-x509', '-newkey', 'rsa:4096',
'-keyout', KEY_FILE, '-out', CERT_FILE,
'-days', '365', '-nodes', '-subj', '/CN=localhost'
], check=True, capture_output=True)
print(f"✓ Certificate generated: {CERT_FILE} and {KEY_FILE}")
except subprocess.CalledProcessError as e:
print(f"✗ Failed to generate certificate: {e.stderr.decode()}", file=sys.stderr)
sys.exit(1)
except FileNotFoundError:
print("✗ Error: openssl not found. Please install OpenSSL.", file=sys.stderr)
sys.exit(1)
def main():
# Check if certificate exists, generate if not
if not os.path.exists(CERT_FILE) or not os.path.exists(KEY_FILE):
generate_certificate()
else:
print(f"Using existing certificate: {CERT_FILE}")
# Create and configure server
server_address = ('localhost', PORT)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
# Create SSL context
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(CERT_FILE, KEY_FILE)
httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
print(f"\n🔒 Serving HTTPS on https://localhost:{PORT}")
print(" Press Ctrl+C to stop\n")
print("Note: Your browser will warn about the self-signed certificate.")
print(" Click 'Advanced' and proceed to localhost.\n")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n\nServer stopped.")
sys.exit(0)
if __name__ == '__main__':
main()