getaddrinfo is missing at socket module

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Hjalmar
Posts: 1
Joined: Thu Dec 30, 2021 11:42 am

getaddrinfo is missing at socket module

Post by Hjalmar » Thu Dec 30, 2021 12:01 pm

Hi,

I'm very new to python so this is probably a stupied question but here we go.

So, i'm having a small project where I'm trying to use a Rest API with the pico raspberry with the small ESP8266 card.
And the wificonnections works very well with the UART module. However when I come to the urequests part i getting some issues.

I'm using the Thonny app at windows to control the pico and micropython, I installed ureqquests from the nice "Managed packages for Raspberry Pi Pico" menu. I tried import urequests and the shell told me to install usocket and socket so from the same menu I choosed the "micropython-socket" package and "micropython-cpython-usocket" package. And I then tried from the shell:

Code: Select all

urequests.request(method="GET",url="http://www.baidu.com/")
And I get:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/urequests.py", line 55, in request
AttributeError: 'module' object has no attribute 'getaddrinfo'
okey, so I open usocket.py and got the small feeling that it was not that usefull:

Code: Select all

import micropython
from socket import *
I went to socket.py and found this:

Code: Select all

from usocket import *
import usocket as _socket

SOCK_SEQPACKET = 5

_GLOBAL_DEFAULT_TIMEOUT = 30
IPPROTO_IP = 0
IP_ADD_MEMBERSHIP = 35
IP_DROP_MEMBERSHIP = 36
INADDR_ANY = 0

error = OSError

def _resolve_addr(addr):
    if isinstance(addr, (bytes, bytearray)):
        return addr
    family = _socket.AF_INET
    if len(addr) != 2:
        family = _socket.AF_INET6
    if addr[0] == "":
        a = "0.0.0.0" if family == _socket.AF_INET else "::"
    else:
        a = addr[0]
    a = getaddrinfo(a, addr[1], family)
    return a[0][4]

def inet_aton(addr):
    return inet_pton(AF_INET, addr)

def create_connection(addr, timeout=None, source_address=None):
    s = socket()
    #print("Address:", addr)
    ais = getaddrinfo(addr[0], addr[1])
    #print("Address infos:", ais)
    for ai in ais:
        try:
            s.connect(ai[4])
            return s
        except:
            pass


class socket(_socket.socket):

    def accept(self):
        s, addr = super().accept()
        addr = _socket.sockaddr(addr)
        return (s, (_socket.inet_ntop(addr[0], addr[1]), addr[2]))

    def bind(self, addr):
        return super().bind(_resolve_addr(addr))

    def connect(self, addr):
        return super().connect(_resolve_addr(addr))

    def sendall(self, *args):
        return self.send(*args)

    def sendto(self, data, addr):
        return super().sendto(data, _resolve_addr(addr))

Now, I can see in the module that getaddrinfo is called for but I can't figure out where it is declaired. Is it missing or I'm I stupied?
Can I solve this some how?

Post Reply