loboris / MicroPython_ESP32_psRAM_LoBo

MicroPython for ESP32 with psRAM support
Other
831 stars 344 forks source link

DNS Resolution for CURL and websockets #286

Open besi opened 5 years ago

besi commented 5 years ago

I am using the curl module and I ran into problems when it comes to DNS resolution

Example 1 using curl

import curl
curl.get('google.com')
(-5, '', '')

No data is loaded.

Example 2: Using sockets:

I have a method called http_get which implements a http GET request in a minimal fashion. Here again if I'm using an IP for the host it works fine. If I use a hostname it won't find the desired server:

# This will work:
http_get("http://192.168.1.124/?play=1")

# The following won't work
http_get("http://myserver.local/?play=1")

The method:

def http_get(url):
    _, _, host, path = url.split('/', 3)
    addr = socket.getaddrinfo(host, 80)[0][-1]
    # The address can't be resolved

    s = socket.socket()
    s.settimeout(3)
    try:
        s.connect(addr)
        s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
        s.close()
    except:
        print("An exception occurred while loading the url %s" % url)

Is there something I missed such as an option that needs enabling for DNS to work? I'm happy to provide more debug information.

mrwhy-orig commented 5 years ago

@besi did you build the fw or are you using a prebuild one?

besi commented 5 years ago

It was built from the master branch