为什么我从Python HTTPLib中获取socket.gaierror:[Errno -2] [英] Why am I getting socket.gaierror: [Errno -2] from Python HTTPLib

查看:198
本文介绍了为什么我从Python HTTPLib中获取socket.gaierror:[Errno -2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python代码非常简单,请在ArduinoYún上创建的网页上进行GET请求.

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()

当我在ArduinoYún的Linux端上运行此命令时, socket.gaierror显示以下错误:[Errno -2]名称或服务未知.但是,当我在Mac上运行相同的脚本时,它就可以正常工作.

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.

我通过将HTTPConnection参数更改为 httplib.HTTPConnection("192.168.240.1")(这是Arduino Yun的IP)来解决了这个问题.

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

那么,为什么这个错误出现在Arduino的Linux端而不是我的Mac上?

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

谢谢.

推荐答案

socket.gaierror来自Python,无法运行getaddrinfo()getnameinfo().在您的情况下,它可能是第一个.该函数需要一个主机和一个端口,并返回一个元组列表,其中包含有关如何连接到主机的信息.如果为该功能指定了主机名,则可以尝试在下面更深入地解析IP地址.

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.

因此,该错误应源于Python无法将您已写入的地址(yun.local)解析为有效的IP地址.我建议您在设备上的/etc/hosts中查看是否在其中定义了它.您也可以尝试使用命令行工具(例如hosttelnet)来检查解析度:

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:

例如:

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

因为它存在于/etc/resolv.conf中,所以它设法将我的localhost解析为::1(IPv6)和127.0.0.1(IPv4).如果我改用您的主机,则:

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

这篇关于为什么我从Python HTTPLib中获取socket.gaierror:[Errno -2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆