Why am I getting socket.gaierror: [Errno -2] from Python HTTPLib

My Python code is very simple, make a GET request on a web page created on Arduino Yún.

import httplib conn = httplib.HTTPConnection("yun.local") conn.request("GET", "/arduino/read/temp/0") r1 = conn.getresponse() print r1.status, r1.reason, r1.read() 

When I run this on the Linux side of Arduino Yún, the following error shows socket.gaierror: [Errno -2] The name or service is unknown. However, when I run the same script on my Mac, it just works fine.

I overcome this problem by changing the HTTPConnection argument to httplib.HTTPConnection ("192.168.240.1"), which is the IP address from Arduino Yun.

So why is this error appearing on the Linux side of the Arduino and not on my Mac?

Thanks.

+6
source share
2 answers

socket.gaierror assumes that Python cannot run getaddrinfo() or getnameinfo() . In your case, this is most likely the first. This function accepts the host and port and returns a list of tuples with information on how to connect to the host. If you specify a host name for this function, try resolving the IP address deeper below.

So, the error should come from the fact that Python will not be able to resolve the address you wrote ( yun.local ) to a valid IP address. I suggest you look at /etc/hosts on the device to see if it is defined there. You can also try using command line tools like host or telnet to check the resolution:

For instance:

 [ pugo@q-bert ~]$ telnet localhost 80 Trying ::1... Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused 

There he managed to resolve my localhost to ::1 (IPv6) and 127.0.0.1 (IPv4), because it exists in /etc/resolv.conf . If I try with your host:

 [ pugo@q-bert ~]$ telnet yun.local 80 telnet: could not resolve yun.local/80: Name or service not known 
+8
source

In my case, I changed the host name. Therefore, I realized that / etc / hosts remained the old host name, I just updated to the new host name in the / etc / hosts file and worked for me.

0
source

Source: https://habr.com/ru/post/969625/


All Articles