Skip to content

In my environment sys.implementation was a namespace instead of a list #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nopnop2002 opened this issue Jun 8, 2024 · 27 comments
Closed

Comments

@nopnop2002
Copy link

nopnop2002 commented Jun 8, 2024

In my environment sys.implementation was a namespace instead of a list.

sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')

So this example will be an error.

$ sudo -E python3 wiznet5k_simpletest.py
Wiznet5k WebClient Test
sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')
type(sys.implementation)=<class 'types.SimpleNamespace'>
Traceback (most recent call last):
  File "/home/pico/blinka/wiznet5k_simpletest.py", line 28, in <module>
    pool = adafruit_connection_manager.get_radio_socketpool(eth)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 155, in get_radio_socketpool
    cp_version = sys.implementation[1]
TypeError: 'types.SimpleNamespace' object is not subscriptable

I think you need to determine if sys.implementation is a list.
https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager/blob/main/adafruit_connection_manager.py#L152

@justmobilize
Copy link
Collaborator

What environment are you using?

@nopnop2002
Copy link
Author

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

@justmobilize
Copy link
Collaborator

And what hardware are you using? Would like to match to see which flow it should take (if it can take the native SSL or not)

@nopnop2002
Copy link
Author

And what hardware are you using?

Luxkfox Pico Max.
Click here for details.
https://wiki.luckfox.com/Luckfox-Pico/Luckfox-Pico-quick-start

@justmobilize
Copy link
Collaborator

Cool. Will find the best way to validate. It doesn't list the Ethernet chip, it's a WIZnet5k?

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 8, 2024

Will find the best way to validate.

thank you.

it's a WIZnet5k?

Yes WIZnet5k

@justmobilize
Copy link
Collaborator

@nopnop2002 when you have a moment, can you try:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import busio
import digitalio
import adafruit_connection_manager
import adafruit_requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K

print("Wiznet5k WebClient Test")

spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

# For Adafruit Ethernet FeatherWing
cs = digitalio.DigitalInOut(board.D10)
# For Particle Ethernet FeatherWing
# cs = digitalio.DigitalInOut(board.D5)

# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs)

print("-------------------------------------")
print("Test 1: does it work without ssl")
print("-------------------------------------")

pool = adafruit_connection_manager.get_radio_socketpool(eth)
requests = adafruit_requests.Session(pool)

HTTP_URL = "http://wifitest.adafruit.com/testwifi/index.html"
print(f"Fetching via http: {HTTP_URL}")
with requests.get(HTTP_URL) as response:
    print(response.text)

adafruit_connection_manager.connection_manager_close_all(release_references=True)

print("-------------------------------------")
print("Test 2: does it work with ssl")
print("-------------------------------------")

import ssl
pool = adafruit_connection_manager.get_radio_socketpool(eth)
ssl_context = ssl.create_default_context()
requests = adafruit_requests.Session(pool, ssl_context)

HTTPS_URL = "https://www.adafruit.com/api/quotes.php"
print(f"Fetching via https: {HTTPS_URL}")
with requests.get(HTTPS_URL) as response:
    print(response.text)

Test 1 will make sure you can make a regular http request and Test 2 with the CPython SSL module.

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 9, 2024

This is the result of trying your code.

$ sudo -E python3 ./wiznet5k_simpletest2.py
Wiznet5k WebClient Test
My IP address is: 192.168.10.145
-------------------------------------
Test 1: does it work without ssl
-------------------------------------
sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')
type(sys.implementation)=<class 'types.SimpleNamespace'>
Fetching via http: http://wifitest.adafruit.com/testwifi/index.html
This is a test of Adafruit WiFi!
If you can read this, its working :)
Traceback (most recent call last):
  File "/home/pico/blinka/./wiznet5k_simpletest2.py", line 40, in <module>
    adafruit_connection_manager.connection_manager_close_all(release_references=True)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 372, in connection_manager_close_all
    for pool in socket_pools:
RuntimeError: dictionary changed size during iteration

this example work fine.

$ sudo -E python3 ./wiznet5k_simpletest.py
Wiznet5k WebClient Test
sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')
type(sys.implementation)=<class 'types.SimpleNamespace'>
My IP address is: 192.168.10.145
IP lookup adafruit.com: 104.20.39.240
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------

Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json
----------------------------------------
{'time': {'updated': 'Jun 9, 2024 22:14:50 UTC', 'updatedISO': '2024-06-09T22:14:50+00:00', 'updateduk': 'Jun 9, 2024 at 23:14 BST'}, 'disclaimer': 'This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org', 'bpi': {'USD': {'code': 'USD', 'rate': '69,741.137', 'description': 'United States Dollar', 'rate_float': 69741.1374}}}
----------------------------------------
Done!

In both cases, the following parts are commented out.

            # Note: At this time, SSL/TLS connections are not supported by older
            # versions of the Wiznet5k library or on boards withouut the ssl module
            # see https://docs.circuitpython.org/en/latest/shared-bindings/support_matrix.html
            ssl_context = None
            """
            cp_version = sys.implementation[1]
            if pool.SOCK_STREAM == 1 and cp_version >= WIZNET5K_SSL_SUPPORT_VERSION:
                try:
                    import ssl  # pylint: disable=import-outside-toplevel

                    ssl_context = ssl.create_default_context()
                except ImportError:
                    # if SSL not on board, default to fake_ssl_context
                    pass
            """

@justmobilize
Copy link
Collaborator

My bad, I forgot getting the pool runs through the same code.

Can you change:

pool = adafruit_connection_manager.get_radio_socketpool(eth)

To

import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool
pool = socketpool.SocketPool(eth)

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

I changed it as follows.

print("-------------------------------------")
print("Test 1: does it work without ssl")
print("-------------------------------------")

#pool = adafruit_connection_manager.get_radio_socketpool(eth)
import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool
pool = socketpool.SocketPool(eth)
requests = adafruit_requests.Session(pool)

HTTP_URL = "http://wifitest.adafruit.com/testwifi/index.html"
print(f"Fetching via http: {HTTP_URL}")
with requests.get(HTTP_URL) as response:
    print(response.text)

adafruit_connection_manager.connection_manager_close_all(release_references=True)

print("-------------------------------------")
print("Test 2: does it work with ssl")
print("-------------------------------------")

import ssl
pool = adafruit_connection_manager.get_radio_socketpool(eth)
ssl_context = ssl.create_default_context()
requests = adafruit_requests.Session(pool, ssl_context)

HTTPS_URL = "https://www.adafruit.com/api/quotes.php"
print(f"Fetching via https: {HTTPS_URL}")
with requests.get(HTTPS_URL) as response:
    print(response.text)

This is the result.

$ sudo -E python3 ./wiznet5k_simpletest2.py
Wiznet5k WebClient Test
My IP address is: 192.168.10.145
-------------------------------------
Test 1: does it work without ssl
-------------------------------------
Fetching via http: http://wifitest.adafruit.com/testwifi/index.html
This is a test of Adafruit WiFi!
If you can read this, its working :)
Traceback (most recent call last):
  File "/home/pico/blinka/./wiznet5k_simpletest2.py", line 42, in <module>
    adafruit_connection_manager.connection_manager_close_all(release_references=True)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 382, in connection_manager_close_all
    key = _global_key_by_socketpool.pop(pool)
KeyError: <adafruit_wiznet5k.adafruit_wiznet5k_socketpool.SocketPool object at 0xa6634bf8>

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

I tried changing it like this.
Force ssl_context to be enabled.

https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager/blob/main/adafruit_connection_manager.py#L152

            # Note: At this time, SSL/TLS connections are not supported by older
            # versions of the Wiznet5k library or on boards withouut the ssl module
            # see https://docs.circuitpython.org/en/latest/shared-bindings/support_matrix.html
            ssl_context = None
            print("sys.implementation={}".format(sys.implementation))
            print("type(sys.implementation)={}".format(type(sys.implementation)))
            """
            cp_version = sys.implementation[1]
            if pool.SOCK_STREAM == 1 and cp_version >= WIZNET5K_SSL_SUPPORT_VERSION:
            if pool.SOCK_STREAM == 1:
                try:
                    import ssl  # pylint: disable=import-outside-toplevel

                    ssl_context = ssl.create_default_context()
                except ImportError:
                    # if SSL not on board, default to fake_ssl_context
                    pass

            if ssl_context is None:
                ssl_context = create_fake_ssl_context(pool, radio)
            """
            import ssl  # pylint: disable=import-outside-toplevel
            ssl_context = ssl.create_default_context()

Fetch with http and https.

HTTP_URL = "http://wifitest.adafruit.com/testwifi/index.html"
HTTPS_URL = "https://www.adafruit.com/api/quotes.php"

# Initialize a requests session
pool = adafruit_connection_manager.get_radio_socketpool(eth)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(eth)
requests = adafruit_requests.Session(pool, ssl_context)

#print("Chip Version:", eth.chip)
#print("MAC Address:", [hex(i) for i in eth.mac_address])
print("My IP address is:", eth.pretty_ip(eth.ip_address))
print(
    "IP lookup adafruit.com: %s" % eth.pretty_ip(eth.get_host_by_name("adafruit.com"))
)


# eth._debug = True
print("Fetching http from", HTTP_URL)
r = requests.get(HTTP_URL)
print("-" * 40)
print(r.text)
print("-" * 40)
r.close()

print()
print("Fetching https from", HTTPS_URL)
r = requests.get(HTTPS_URL)
print("-" * 40)
print(r.json())
print("-" * 40)
r.close()

print("Done!")

Test 1 will make sure you can make a regular http request and Test 2 with the CPython SSL module.

regular http request is ok.
https request is ng.

$ sudo -E python3 ./wiznet5k_simpletest3.py
Wiznet5k WebClient Test
pool=<adafruit_wiznet5k.adafruit_wiznet5k_socketpool.SocketPool object at 0xa65d2c40>
sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')
type(sys.implementation)=<class 'types.SimpleNamespace'>
My IP address is: 192.168.10.145
IP lookup adafruit.com: 104.20.39.240
Fetching http from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------

Fetching https from https://www.adafruit.com/api/quotes.php
Traceback (most recent call last):
  File "/home/pico/blinka/./wiznet5k_simpletest3.py", line 53, in <module>
    r = requests.get(HTTPS_URL)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 683, in get
    return self.request("GET", url, **kw)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 609, in request
    socket = self._connection_manager.get_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 340, in get_socket
    socket = self._get_connected_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 243, in _get_connected_socket
    socket = ssl_context.wrap_socket(socket, server_hostname=host)
  File "/usr/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.10/ssl.py", line 1018, in _create
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
AttributeError: 'Socket' object has no attribute 'getsockopt'. Did you mean: 'setsockopt'?

ssl import is successful

$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl

AttributeError: 'Socket' object has no attribute 'getsockopt'. Did you mean: 'setsockopt'?

I can't understand.

$ vi /usr/lib/python3.10/ssl.py
    def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
                suppress_ragged_eofs=True, server_hostname=None,
                context=None, session=None):
        if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
            raise NotImplementedError("only stream sockets are supported")

@justmobilize
Copy link
Collaborator

So here is the error for SSL:

  File "/usr/lib/python3.10/ssl.py", line 1018, in _create
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
AttributeError: 'Socket' object has no attribute 'getsockopt'. Did you mean: 'setsockopt'?

That's the built in SSL module trying to get the socket type from the WIZNet5k socket and getsockopt isn't implemented.

You could add the following to adafruit_wiznet5k_socketpool.py:

    @_check_socket_closed
    def getsockopt(self, level, opt):
        if level == SocketPool.SOL_SOCKET and opt == SocketPool.SOCK_STREAM:
            return self._sock_type
        raise OSError

And see if that gets you any further

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

adafruit_wiznet5k_socketpool.py has 2 class.

class SocketPool:

class Socket:

Which should I add this to?

    @_check_socket_closed
    def getsockopt(self, level, opt):
        if level == SocketPool.SOL_SOCKET and opt == SocketPool.SOCK_STREAM:
            return self._sock_type
        raise OSError

I added this to Socket class.

    @property
    @_check_socket_closed
    def proto(self):
        """Socket protocol (always 0x00 in this implementation)."""
        return 0

    @_check_socket_closed
    def getsockopt(self, level, opt):
        if level == SocketPool.SOL_SOCKET and opt == SocketPool.SOCK_STREAM:
            return self._sock_type
        raise OSError
$ sudo -E python3 ./wiznet5k_simpletest3.py
Wiznet5k WebClient Test
pool=<adafruit_wiznet5k.adafruit_wiznet5k_socketpool.SocketPool object at 0xa6672c40>
sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')
type(sys.implementation)=<class 'types.SimpleNamespace'>
My IP address is: 192.168.10.145
IP lookup adafruit.com: 104.20.39.240
Fetching http from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------

Fetching https from https://www.adafruit.com/api/quotes.php
Traceback (most recent call last):
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 340, in get_socket
    socket = self._get_connected_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 243, in _get_connected_socket
    socket = ssl_context.wrap_socket(socket, server_hostname=host)
  File "/usr/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.10/ssl.py", line 1018, in _create
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 288, in wrapper
    return func(self, *args, **kwargs)  # pylint: disable=not-callable
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 816, in getsockopt
    raise OSError
OSError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pico/blinka/./wiznet5k_simpletest3.py", line 53, in <module>
    r = requests.get(HTTPS_URL)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 683, in get
    return self.request("GET", url, **kw)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 609, in request
    socket = self._connection_manager.get_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 350, in get_socket
    socket = self._get_connected_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 243, in _get_connected_socket
    socket = ssl_context.wrap_socket(socket, server_hostname=host)
  File "/usr/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.10/ssl.py", line 1018, in _create
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 288, in wrapper
    return func(self, *args, **kwargs)  # pylint: disable=not-callable
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 816, in getsockopt
    raise OSError
OSError

@justmobilize
Copy link
Collaborator

Can you print level and opt in the new method we added?

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

    @_check_socket_closed
    def getsockopt(self, level, opt):
        print("getsockopt level={}".format(level))
        print("getsockopt opt={}".format(opt))
        print("getsockopt SocketPool.SOL_SOCKET={}".format(SocketPool.SOL_SOCKET))
        print("getsockopt SocketPool.SOCK_STREAM={}".format(SocketPool.SOCK_STREAM))
        if level == SocketPool.SOL_SOCKET and opt == SocketPool.SOCK_STREAM:
            return self._sock_type
        raise OSError
IP lookup adafruit.com: 104.20.38.240
Fetching http from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------

Fetching https from https://www.adafruit.com/api/quotes.php
getsockopt level=1
getsockopt opt=3
getsockopt SocketPool.SOL_SOCKET=4095
getsockopt SocketPool.SOCK_STREAM=1
getsockopt level=1
getsockopt opt=3
getsockopt SocketPool.SOL_SOCKET=4095
getsockopt SocketPool.SOCK_STREAM=1
Traceback (most recent call last):
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 340, in get_socket
    socket = self._get_connected_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 243, in _get_connected_socket
    socket = ssl_context.wrap_socket(socket, server_hostname=host)
  File "/usr/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.10/ssl.py", line 1018, in _create
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 288, in wrapper
    return func(self, *args, **kwargs)  # pylint: disable=not-callable
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 820, in getsockopt
    raise OSError
OSError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pico/blinka/./wiznet5k_simpletest3.py", line 53, in <module>
    r = requests.get(HTTPS_URL)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 683, in get
    return self.request("GET", url, **kw)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 609, in request
    socket = self._connection_manager.get_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 350, in get_socket
    socket = self._get_connected_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 243, in _get_connected_socket
    socket = ssl_context.wrap_socket(socket, server_hostname=host)
  File "/usr/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.10/ssl.py", line 1018, in _create
    if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 288, in wrapper
    return func(self, *args, **kwargs)  # pylint: disable=not-callable
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py", line 820, in getsockopt
    raise OSError
OSError

@justmobilize
Copy link
Collaborator

Ahh, by mistake, since it's passing in SOL_SOCKET, SO_TYPE, although in CPython I get:

> print(socket.SOL_SOCKET, socket.SO_TYPE)
65535 4104

Which isn't 1 3...

Just try always returning self._sock_type

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

Just try always returning self._sock_type

I'm not native.
What does this mean?

@justmobilize
Copy link
Collaborator

Sorry about that:

    @_check_socket_closed
    def getsockopt(self, level, opt):
        return self._sock_type

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

I changed it like this.

    @_check_socket_closed
    def getsockopt(self, level, opt):
        return self._sock_type
IP lookup adafruit.com: 104.20.38.240
Fetching http from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------

Fetching https from https://www.adafruit.com/api/quotes.php
Traceback (most recent call last):
  File "/home/pico/blinka/./wiznet5k_simpletest3.py", line 53, in <module>
    r = requests.get(HTTPS_URL)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 683, in get
    return self.request("GET", url, **kw)
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_requests.py", line 609, in request
    socket = self._connection_manager.get_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 340, in get_socket
    socket = self._get_connected_socket(
  File "/home/pico/.local/lib/python3.10/site-packages/adafruit_connection_manager.py", line 243, in _get_connected_socket
    socket = ssl_context.wrap_socket(socket, server_hostname=host)
  File "/usr/lib/python3.10/ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.10/ssl.py", line 1032, in _create
    fileno=sock.fileno()
AttributeError: 'Socket' object has no attribute 'fileno'

I'm going to work now, so my reply will be delayed.

@justmobilize
Copy link
Collaborator

Okay. So to support CPython SSL via the WIZNet5k will take some work.

Are you needing SSL?

@nopnop2002
Copy link
Author

Are you needing SSL?

I don't need it right away.

Please spend a lot of time working on it.

@justmobilize
Copy link
Collaborator

Okay, I'll open up a PR here to handle this case, and treat it like SSL isn't supported.

If willing, can you please open a new issue here: https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k for supporting CPython SSL?

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 10, 2024

treat it like SSL isn't supported.

That's fine.

In my environment sys.implementation was a namespace instead of a list.

Let's get back to this problem.


can you please open a new issue here

adafruit/Adafruit_CircuitPython_Wiznet5k#166

@justmobilize
Copy link
Collaborator

justmobilize commented Jun 10, 2024

@nopnop2002 can you try the code from here: #21?

No SSL, but will work with HTTP requests.

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 11, 2024

I tested your code.

There will be no error.

My implementation_name=cpython
My implementation_version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)

$ python3 --version
Python 3.10.12

I added print.

            print("sys.implementation={}".format(sys.implementation))
            print("type(sys.implementation)={}".format(type(sys.implementation)))
            implementation_name = sys.implementation.name
            implementation_version = sys.implementation.version
            print("pool.SOCK_STREAM={}".format(pool.SOCK_STREAM))
            print("implementation_name={}".format(implementation_name))
            print("implementation_version={}".format(implementation_version))
            print("WIZNET5K_SSL_SUPPORT_VERSION={}".format(WIZNET5K_SSL_SUPPORT_VERSION))
            if (
                pool.SOCK_STREAM == 1
                and implementation_name == "circuitpython"
                and implementation_version >= WIZNET5K_SSL_SUPPORT_VERSION
            ):
                try:
                    import ssl  # pylint: disable=import-outside-toplevel

                    ssl_context = ssl.create_default_context()
                except ImportError:
                    # if SSL not on board, default to fake_ssl_context
                    pass

            if ssl_context is None:
                ssl_context = create_fake_ssl_context(pool, radio)

You may need to change it for cpython , micropython and circuitpython environments.

$ sudo -E python3 ./wiznet5k_simpletest.py
Wiznet5k WebClient Test
pool=<adafruit_wiznet5k.adafruit_wiznet5k_socketpool.SocketPool object at 0xa64a0898>
sys.implementation=namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0), hexversion=50990320, _multiarch='arm-linux-gnueabihf')
type(sys.implementation)=<class 'types.SimpleNamespace'>
pool.SOCK_STREAM=1
implementation_name=cpython
implementation_version=sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)
WIZNET5K_SSL_SUPPORT_VERSION=(9, 1)
My IP address is: 192.168.10.145
IP lookup adafruit.com: 104.20.39.240
Fetching text from http://wifitest.adafruit.com/testwifi/index.html
----------------------------------------
This is a test of Adafruit WiFi!
If you can read this, its working :)
----------------------------------------

Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json
----------------------------------------
{'time': {'updated': 'Jun 11, 2024 11:09:01 UTC', 'updatedISO': '2024-06-11T11:09:01+00:00', 'updateduk': 'Jun 11, 2024 at 12:09 BST'}, 'disclaimer': 'This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org', 'bpi': {'USD': {'code': 'USD', 'rate': '66,914.808', 'description': 'United States Dollar', 'rate_float': 66914.8083}}}
----------------------------------------
Done!

@nopnop2002
Copy link
Author

nopnop2002 commented Jun 11, 2024

@justmobilize

This issue will be closed once the your draft code is merged.

The SSL issue will be carried over into another issue.

Thank you for your efforts.

@justmobilize
Copy link
Collaborator

Fixed with #21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants