@deron
You can create a simple HTTPS server in Python using the http.server
module and the ssl
module. Here's a step-by-step guide to creating a simple HTTPS server in Python:
1
|
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem |
This will generate a self-signed SSL certificate (cert.pem
) and key (key.pem
) that you can use to secure your HTTPS server.
1 2 3 4 5 6 7 8 9 |
import http.server import ssl server_address = ('localhost', 8443) httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler) httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, certfile='./cert.pem', keyfile='./key.pem') print("Starting HTTPS server on {}:{}".format(server_address[0], server_address[1])) httpd.serve_forever() |
This code sets up an HTTPS server on localhost
at port 8443
using a self-signed SSL certificate and key. The server serves files using the SimpleHTTPRequestHandler
.
1
|
python https_server.py |
This will start the HTTPS server and you should be able to access it in your web browser using https://localhost:8443
.
Please note that for production use, you should consider using a properly signed SSL certificate from a trusted certificate authority.