Any working examples of an HTTPS web server?



  • Like it says, I’m interested in running an https web server on a WiPy 2, and having trouble connecting. Things work fine with plain http. Here’s my sample code:

    def http_server():
            bindsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
            bindsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            bindsocket.bind(("0.0.0.0", 443))
            bindsocket.listen()
        
            while running:
                connection, client = bindsocket.accept()
                connection = ssl.wrap_socket(connection, keyfile="/flash/cert/key.pem", certfile="/flash/cert/cert.pem", server_side=True)
                while True:
                    line = connection.readline()
                    if not line or line == b"\r\n":
                        break
                connection.write("HTTP/1.1 200 OK\r\n")
                connection.write("Content-Type: application/json\r\n")
                connection.write("Access-Control-Allow-Origin: *\r\n")
                connection.write("\r\n")
        
                connection.write('{"temperature/ambient": {"value": %r, "units": "degC"}, "humidity/ambient": {"value": %r, "units": "RH"}, "pressure/barometric": {"value": %r, "units": "Pa"}}' % (data.temperature_ambient, data.humidity_ambient, data.pressure_barometric))
                connection.close()
                flash(0x00ff00)
        
            print("http_server stopped.")    
    

    When I connect with a web browser, I’m prompted to accept my (self-signed) certificate so something’s working right, but the connection hangs. On the WiPy side, I get the following backtrace:

    Unhandled exception in thread started by <function http_server at 0x3ffe81a0>
    Traceback (most recent call last):
      File "server.py", line 53, in http_server
    OSError: -1
    

    Line 53 is where I’m wrapping the socket:

    connection = ssl.wrap_socket(connection, keyfile="/flash/cert/key.pem", certfile="/flash/cert/cert.pem", server_side=True)
    

    Any thoughts?

    Thanks,
    Tim



  • Following-up … if I add an exception handler and keep trying to connect, I get a bunch of different error numbers:

    Uncaught exception: -17168
    Uncaught exception: -10368
    Uncaught exception: -10368
    Uncaught exception: -10368
    Uncaught exception: -1
    Uncaught exception: -32512
    Uncaught exception: -29312
    Uncaught exception: -16
    Uncaught exception: -10368
    Uncaught exception: -4480
    Uncaught exception: -19840
    Uncaught exception: -4480
    Uncaught exception: -16912
    

    Any advice on how / where to look those up?

    Cheers,
    Tim



Pycom on Twitter