How to make HTTPS requests
-
Hello, is there a way to make HTTPS requests?
I have tried many different ways, but even wrapping a SSL socket generated errors. I am really confused right now.. I have LoPy with the latest firmware (1.6.13.b1).
-
Does it work reliably if repeated for hundreds of requests? Properly raised exceptions are acceptable in my opinion.
-
I want to share with you the code that worked for me afterall:
=========================================================================
import socket, ssl
server = ('192.168.0.105', 25566)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #def socket.IPPROTO_TCP
sslsock = ssl.wrap_socket(sock)
sslsock.connect(server)
sslsock.send('GET /ping HTTP/1.0\r\n\r\n')
print(sslsock.recv(200))
sslsock.close() #automatically closes underlying socket==============================================
import urequests
url = 'https://192.168.0.105:25566/ping'
resp = urequests.request('GET', url)
print(resp.content)===============================================
import urequests
url = 'https://192.168.0.105:25566/register'
headers = {'content-type': 'application/json'}
data = {'email':'email@gmail.com', 'password':'password', 'fullName':'My Name'}resp = urequests.request('POST', url, json=data, headers=headers)
print(resp.content)======================================================================
I still get OSERROR or EHOSTUNREACH from time to time, especially when I try to make these requests one after another. My guess is that there are some resources that are not freed when closing the socket.
-
I hope that the issue will be fixed soon, however it is fully in hands of Pycom team or maybe something will have to be fixed by Espressif in esp-idf.
You could try to write your own library which will compile requests and parse responses, however if the issue is caused e.g. by ssl.wrap_socket() you will face similar behavior.
-
Thanks,
Will these be implemented officially in the near future? What alternatives do I have?
I guess I could do well with TCP + separate encryption, right?
-
I could do POST over HTTPS using urequests.py library, however it is unstable/unreliable:
https://forum.pycom.io/topic/1061/https-post-using-urequests-py-and-guru-mediation-error/
It works good enough for doing experiments but not for long-running applications.
-
@lebucur SSL should work if the WiPY (LoPy) is the client. The problem with ssl.wrap_socket() arises when the WiPy (LoPy) is the Server Side. Try also to use the urequests.py lib.