Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My Python code is very simple, make a GET request on a webpage created on an 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 the Arduino Yún, the following error shows up socket.gaierror: [Errno -2] Name or service not known. 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 from the Arduino Yun.

So, why is this error showing up on the Linux side of Arduino and not in my Mac?

Thanks.

like image 395
Ricardo Garzo Avatar asked May 21 '14 07:05

Ricardo Garzo


1 Answers

The socket.gaierror comes from Python not being able to run getaddrinfo() or getnameinfo(). In your case it is likely the first one. That function takes a host and a port and returns a list of tuples with information about how to connect to a host. If you give a host name to that function there will be a try to resolve the IP address deeper below.

So, the error should come from Python not being able to resolve the address you have written (yun.local) to a valid IP address. I would suggest you look in /etc/hosts on the device to see if it is defined there. You can also try with command line tools such as host or telnet, just to check the resolving:

For example:

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

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

[pugo@q-bert ~]$ telnet yun.local 80
telnet: could not resolve yun.local/80: Name or service not known
like image 150
Anders Piniesjö Avatar answered Sep 22 '22 21:09

Anders Piniesjö