Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Any working examples of an HTTPS web server?

    Scheduled Pinned Locked Moved WiPy 2&3
    wipy 2webserverhttpstls
    2 Posts 1 Posters 553 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T Offline
      tshead
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • T Offline
        tshead
        last edited by

        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

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • 1 / 1
        • First post
          Last post

        Pycom on Twitter

        Pycom