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

    Q: ussl usage for https - problem

    Scheduled Pinned Locked Moved WiPy 2&3
    8 Posts 3 Posters 3.7k Views 4 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.
    • U Offline
      ubiq_01 Pybytes Beta
      last edited by ubiq_01

      It was good to see ussl being integrated in the recent firmware. I tried to use it with code that worked for MicroPython on the WiPy 1.0 / CC3200 and the UNIX MicroPython version. Code is below. Unfortunately, on WiPy 2.0 the code fails at the read() - with a -28928 (I didn't check whether / how the request arrives at the https server yet). See https://twitter.com/_rac01/status/801484003845017600

      My question is: Did somebody successfully use the ussl module? Is there any test code / server side to try with?

      best regards
      Ralf

      # https_get.py
      
      import sys
      import os
      import struct
      try:
          import usocket as socket
      except ImportError:
          import socket
      import ussl
      
      def main():
      
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      
          # WiPy 1.0 / CC3200 had and needed socket.IPPROTO_SEC
          # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
      
          host="www.ubiqkom.org"
          path="/index.html"
      
          port=443
          ai = socket.getaddrinfo(host, port)
          addr = ai[0][4]
      
          s.connect(addr)
          print("connect() done !")
      
          # MicroPython on UNIX version
          # s = ussl.wrap_socket(s)
      
          # WiPy version
          s = ussl.wrap_socket(s, keyfile=None, certfile=None, server_side=False, cert_reqs=ussl.CERT_NONE)
      
          request='GET ' + path + ' HTTP/1.1\r\n'
          request=request + 'Host: ' + host + '\r\n'
          request=request + '\r\n'
      
          print('request is: \n' + request)
          s.write(request)
      
          print('Start reading !')
          while True:
      	    line = s.readline()
      	    print('result: ' + str(line))
          if line == b'':
      		    break
      
          s.close()
      
      # main()
      
      1 Reply Last reply Reply Quote 0
      • D Offline
        daniel
        last edited by

        Hello,

        We have used succesfully to connect to AWS IoT using MQTT. I'll test your example and see why it fails. Thanks for the report.

        Cheers,
        Daniel

        R 1 Reply Last reply Reply Quote 0
        • U Offline
          ubiq_01 Pybytes Beta
          last edited by

          Hello Daniel,

          great to hear. For AWS IoT - did you use native MQTT or the MQTT over Websockets endpoint they also provide? And which code (umqtt?) on the WiPy? I'd be very interested to also reproduce this.

          best regards
          Ralf

          1 Reply Last reply Reply Quote 0
          • D Offline
            daniel
            last edited by

            @ubiq_01: I found the problem. At this moment we are only doing the SSL/TLS handshake during the connection (if the socket is already wrapped), but it should be done while wrapping the socket if the socket is already connected. We'll fix this quickly, before today's release. In the meantime, if you wrap the socket, and after that call s.connect(), it will work :-).

            Cheers,
            Daniel

            1 Reply Last reply Reply Quote 1
            • D Offline
              daniel
              last edited by

              So, like this:

              # https_get.py
              
              import sys
              import os
              import struct
              try:
                  import usocket as socket
              except ImportError:
                  import socket
              import ussl
              
              def main():
              
                  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              
                  # WiPy 1.0 / CC3200 had and needed socket.IPPROTO_SEC
                  # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
              
                  host="www.ubiqkom.org"
                  path="/index.html"
              
                  port=443
                  ai = socket.getaddrinfo(host, port)
                  addr = ai[0][4]
              
                  # MicroPython on UNIX version
                  # s = ussl.wrap_socket(s)
              
                  # WiPy version
                  s = ussl.wrap_socket(s, keyfile=None, certfile=None, server_side=False, cert_reqs=ussl.CERT_NONE)
              
                  s.connect(addr)
                  print("connect() done !")
              
                  request='GET ' + path + ' HTTP/1.1\r\n'
                  request=request + 'Host: ' + host + '\r\n'
                  request=request + '\r\n'
              
                  print('request is: \n' + request)
                  s.write(request)
              
                  print('Start reading !')
                  while True:
              	    line = s.readline()
              	    print('result: ' + str(line))
                  if line == b'':
              		    break
              
                  s.close()
              
              # main()
              
              1 Reply Last reply Reply Quote 0
              • D Offline
                daniel
                last edited by

                @ubiq_01 : Solved with https://github.com/pycom/pycom-micropython/commit/46f6ee00a422a3b161092bf7bb79af3dbcca6aad

                Will be available later today when we release.

                1 Reply Last reply Reply Quote 0
                • R Offline
                  RSK @daniel
                  last edited by

                  @daniel I am trying to push data from LoPy to AWS IoT using MQTT.

                  I am little confused on process generating & passing the certification string to AWS for authentication. By referring the below links.

                  https://www.inqdo.com/wp-content/uploads/2016/05/aws-summit-IoT-by-inQdo.pdf
                  https://forum.micropython.org/viewtopic.php?t=2066

                  Since you have already experimented this, It would be great help if the sample code using MQTT native or MQTT over Websocket is shared across.

                  Thanks & Regards,
                  Satish Kumar. R.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    daniel
                    last edited by

                    Hello Satish,

                    Please check the MQTT example that I posted in https://forum.pycom.io/topic/352/aws-iot-device-support-for-lopy/5

                    Cheers,
                    Daniel

                    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