Q: ussl usage for https - problem


  • Pybytes Beta

    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()

    Log in to reply
     


  • 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



  • @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.





  • 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()
    


  • @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


  • Pybytes Beta

    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



  • 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


Log in to reply
 

4 out of 8

Pycom on Twitter