-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_server.py
More file actions
30 lines (23 loc) · 785 Bytes
/
https_server.py
File metadata and controls
30 lines (23 loc) · 785 Bytes
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
import http.server
import ssl
import os
# Define the server address and port
HOST = 'localhost'
PORT = 4443
# Change directory to the project folder
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Create the HTTP server
httpd = http.server.HTTPServer((HOST, PORT), http.server.SimpleHTTPRequestHandler)
# Create an SSL context
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile="cert.pem", keyfile="key.pem")
# Wrap the server socket with SSL
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
print(f"Serving on https://{HOST}:{PORT}")
print("Press Ctrl+C to stop the server.")
# Start the server
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down the server...")
httpd.server_close()