Closed dhalbert closed 3 years ago
If you use requests.set_socket(socket, esp)
like the PyPortal library does, it should work correctly. Maybe the examples just need to be updated?
I think that's true, but maybe the examples used to work (by accident??) The old code is this:
class _FakeSSLContext:
@staticmethod
def wrap_socket(socket, server_hostname=None):
"""Return the same socket"""
# pylint: disable=unused-argument
return socket
def set_socket(sock, iface=None):
"""Legacy API for setting the socket and network interface. Use a `Session` instead."""
global _default_session # pylint: disable=global-statement,invalid-name
_default_session = Session(sock, _FakeSSLContext())
if iface:
sock.set_interface(iface)
wrap_socket()
doesn't use the iface
, so it won't have an error.
Is the socket.set_interface()
just unnecessary if you do requests.set_socket(socket, esp)
? I am just not familiar with the idioms for using the library.
Hmm, perplexing. 🤔
It does work to replace the two lines with the one. (The point of all this was just to test that the revised requests
library still worked on AirLift with SSL.)
Here's what I do:, and it works in CircuitPython. There may be a different sequence to be fully compatible with CPython:
# SP32-S2:
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# ESP32SPI:
# Requests (legacy API)
requests.set_socket(socket, esp)
I don't know of a single code sequence that works in CircuitPython on both non-ESP32-S2 and ESP32-S2. The Requests import is typically different between the two also. I suspect Scott is aiming for CPython compatibility on all platforms as he reworks the legacy stack.
@dhalbert I think the simplest thing is to have set_socket
pull the interface from the socket if it isn't provided. It's all a hack for backwards compatibility so we can do sketchy things in it.
I'm not sure whether this is an error in the examples or in the revised legacy API.
examples/requests_simpletest.py
andexamples/requests_advanced.py
both contain code:However, this code stopped working at some point during the revision of the legacy API. If I try the
simpletest
, I getHere is the relevant revised code:
The
_FakeSSLContext
ends up with aNone
iface
, and thenwrap_socket()
fails becauseself._iface.TLS_MODE
will fail.