Python gaierror: [Errno 8] nodename 或 servname 提供,或未知(使用 macOS Sierra)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39970606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gaierror: [Errno 8] nodename nor servname provided, or not known (with macOS Sierra)
提问by FBL
socket.gethostbyname(socket.gethostname()) worked well on OS X El Capitan. However, it's not working now after the Mac updated to macOS Sierra.
socket.gethostbyname(socket.gethostname()) 在 OS X El Capitan 上运行良好。但是,在 Mac 更新到 macOS Sierra 后,它现在不起作用。
Thanks!
谢谢!
import socket
socket.gethostbyname(socket.gethostname())
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
socket.gethostbyname(socket.gethostname())
gaierror: [Errno 8] nodename nor servname provided, or not known
回答by Armut
There is an answer in OP's comments based on another question,which requires to edit /etc/hosts
, i.e echo 127.0.0.1 $HOSTNAME >> /etc/hosts
.
OP 的评论中有一个基于另一个问题的答案,该问题需要编辑/etc/hosts
,即echo 127.0.0.1 $HOSTNAME >> /etc/hosts
.
Alternatively, you can use socket.gethostbyname('localhost')
, assuming that localhost
is what you need and it is defined in your /etc/hosts
.
或者,您可以使用socket.gethostbyname('localhost')
,假设这localhost
是您需要的并且在您的/etc/hosts
.
回答by jxc
Same problem tome. I change the code to:
同样的问题。我将代码更改为:
import socket
socket.gethostbyname("")
And it works now.
它现在有效。
回答by Chris Reyes - he-him
I had the same problem today (with MacOS Mojave) and was able to fix it using the linkthat @FBLprovided in the comments.
今天我有同样的问题(和MacOS莫哈韦),并能使用解决它链接的是@FBL在给予。
Specifically, I just had to go to System Preferences -> Sharing and enable sharing. (I enabled printer sharing). Then I verified it was working via ping $HOST
. Afterwards, I was able to turn sharing back off and everything (both ping $HOST
and python -c 'import socket; print socket.gethostbysocketname(socket.gethostname())
) still worked.
具体来说,我只需转到“系统偏好设置”->“共享”并启用共享。(我启用了打印机共享)。然后我验证它是通过ping $HOST
. 之后,我能够关闭共享,一切(ping $HOST
和python -c 'import socket; print socket.gethostbysocketname(socket.gethostname())
)仍然有效。
回答by JJ-Pysquared
If you are using Jupyter notebook or not try this
如果你正在使用 Jupyter notebook 或者不试试这个
IP4=str(ipaddress.ip_address(8888))
IPAddr = socket.gethostbyname(IP4)
don't forget to import ipaddress & socket
不要忘记导入 ipaddress 和 socket
回答by a_dino
I was having the same issue. I was not able to ping localhost.
我遇到了同样的问题。我无法 ping 本地主机。
ping localhost
ping 本地主机
>> error ping: cannot resolve localhost: Unknown host
I changed my hosts file.
我更改了我的主机文件。
sudo nano /etc/hosts
须藤纳米 /etc/hosts
Add this to the file (use Tab/4 spaces to separate>> 127.0.0.1{tab/4spaces}localhost) Save it and restart the terminal.
将此添加到文件中(使用 Tab/4 空格分隔>> 127.0.0.1{tab/4spaces}localhost)保存并重新启动终端。
127.0.0.1 localhost
127.0.0.1 本地主机
To confirm if everything works fine, try pinging your localhost
要确认一切是否正常,请尝试 ping 本地主机
ping localhost
ping 本地主机