[Python] How to make a simple HTTPS server in Python 3x

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    How can i create the simpliest python server, which will receive just one response and than die?

    I've tried this, but modified it a bit, because of deprecation of some methods.

    import http.server
    from ssl import SSLContext


    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, http.server.SimpleHTTPRequestHandler)
    httpd.socket = SSLContext().wrap_socket(sock=httpd.socket,
    server_side=True,
    do_handshake_on_connect=False,
    suppress_ragged_eofs=True)
    httpd.serve_forever()


    But it doesn't work.

    C:\Users\mikha\Desktop\Mika\Projects\yummy_slack\test_1.py:14: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated.
    httpd.socket = SSLContext().wrap_socket(sock=httpd.socket,
    C:\Users\mikha\Desktop\Mika\Projects\yummy_slack\test_1.py:14: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated
    httpd.socket = SSLContext().wrap_socket(sock=httpd.socket,
    ----------------------------------------
    Exception occurred during processing of request from ('127.0.0.1', 51828)
    Traceback (most recent call last):
    File "C:\Program Files\Python310\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
    File "C:\Program Files\Python310\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
    File "C:\Program Files\Python310\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
    File "C:\Program Files\Python310\lib\http\server.py", line 668, in __init__
    super().__init__(*args, **kwargs)
    File "C:\Program Files\Python310\lib\socketserver.py", line 747, in __init__
    self.handle()
    File "C:\Program Files\Python310\lib\http\server.py", line 433, in handle
    self.handle_one_request()
    File "C:\Program Files\Python310\lib\http\server.py", line 401, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
    File "C:\Program Files\Python310\lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
    File "C:\Program Files\Python310\lib\ssl.py", line 1274, in recv_into
    return self.read(nbytes, buffer)
    File "C:\Program Files\Python310\lib\ssl.py", line 1130, in read
    return self._sslobj.read(len, buffer)
    ssl.SSLError: [SSL: NO_SHARED_CIPHER] no shared cipher (_ssl.c:2578)
    ----------------------------------------


    And this error continues for a bunch of ports in a row.

    I'm not a genius in networks, but really trying to understand what's going wrong here.

    The main task is to receive the redirected response from Slack OAuth link.

    UPD: I've tried to use certificate and key, when starting server. But script stucks on load_cert_chain. No crashes or something.

    I've used this command to create a certificate and key openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 Here the example:

    import http.server
    import ssl


    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 = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    context.load_cert_chain(certfile="cert.pem", keyfile="key.pem")

    httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
    httpd.serve_forever()

    Continue reading...

Compartilhe esta Página