Created
November 10, 2024 20:47
-
-
Save danilw/21c84193d1a180bc7232541789ad8635 to your computer and use it in GitHub Desktop.
Python 3 https server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout key.pem -out cert.pem | |
# python3 server.py | |
import http.server | |
import ssl | |
def get_ssl_context(certfile, keyfile): | |
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) | |
context.load_cert_chain(certfile, keyfile) | |
context.set_ciphers("@SECLEVEL=1:ALL") | |
return context | |
class MyHandler(http.server.SimpleHTTPRequestHandler): | |
def do_POST(self): | |
content_length = int(self.headers["Content-Length"]) | |
post_data = self.rfile.read(content_length) | |
print(post_data.decode("utf-8")) | |
server_address = ("127.0.0.1", 5000) | |
httpd = http.server.HTTPServer(server_address, MyHandler) | |
context = get_ssl_context("cert.pem", "key.pem") | |
httpd.socket = context.wrap_socket(httpd.socket, server_side=True) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment