adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 37 forks source link

Host cannot be an IP address #35

Closed kevinaj closed 4 years ago

kevinaj commented 4 years ago

Using an IP address as a host results in an attempt to resolve the IP address via DNS.

src:

import board
import busio
from digitalio import DigitalInOut
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_requests as requests

spi = busio.SPI(clock=board.SCK, MOSI=board.MOSI, MISO=board.MISO)
eth = WIZNET5K(spi, DigitalInOut(board.D10))

socket.set_interface(eth)
requests.set_socket(socket)

response = requests.get('http://10.0.0.1')
print(response.text)

output:

Traceback (most recent call last):
  File "code.py", line 14, in <module>
  File "/lib/adafruit_requests.py", line 605, in get
  File "/lib/adafruit_requests.py", line 478, in request
  File "/lib/adafruit_requests.py", line 413, in _get_socket
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_socket.py", line 80, in getaddrinfo
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_socket.py", line 88, in gethostbyname
  File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 268, in get_host_by_name
AssertionError: Failed to resolve hostname!
kevinaj commented 4 years ago

Looking further, it's possible this is a wiznet5k issue. Requests defers the entire host portion of the URL to the socket for resolution:

socket = self._get_socket(host, port, proto, timeout=timeout)

Ultimately this falls all the way through adafruit_wiznet5k_socket.getaddrinfo(), adafruit_wiznet5k.get_host_by_name(), and finally adafruit_wiznet5k_dns.gethostbyname(). At no point is there any check to see if the host is an IP and not a name.

kevinaj commented 4 years ago

Solved this with https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/pull/23