简体   繁体   English

gaierror: [Errno 8] nodename 或 servname 提供,或未知(使用 macOS Sierra)

[英]gaierror: [Errno 8] nodename nor servname provided, or not known (with macOS Sierra)

socket.gethostbyname(socket.gethostname()) worked well on OS X El Capitan. socket.gethostbyname(socket.gethostname()) 在 OS X El Capitan 上运行良好。 However, it's not working now after the Mac updated to macOS Sierra.但是,在 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

There is an answer in OP's comments based on another question, which requires to edit /etc/hosts , ie 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定义。

Same problem tome.同样的问题。 I change the code to:我将代码更改为:

import socket
socket.gethostbyname("")

And it works now.它现在有效。

I had the same problem today (with MacOS Mojave) and was able to fix it using the link that @FBL provided in the comments.我今天遇到了同样的问题(使用 MacOS Mojave)并且能够使用@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 .然后我通过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 $HOSTpython -c 'import socket; print socket.gethostbysocketname(socket.gethostname()) )仍然有效。

It seems like a python bug?这似乎是一个python错误? You need to set the hostname & ipv4 , after this, you can fix it.您需要设置主机名和 ipv4 ,在此之后,您可以修复它。

import socket
hostname = socket.gethostname()
print(hostname)  >> samzong

# make sure do it
!echo "127.0.0.1 samzong" >> /private/etc/hosts

Rather than using the localhost, use the computer name as specified in /etc/hosts.不要使用本地主机,而是使用 /etc/hosts 中指定的计算机名称。

For example, /etc/host would have something like this:例如, /etc/host 会有这样的内容:

127.0.0.1 ET02282-MAC.local
127.0.0.1 localhost

And in your connection use "ET02282-MAC.local"并在您的连接中使用“ET02282-MAC.local”

另一个简单的解决方案是转到终端粘贴 sudo nano /etc/hosts 找到您需要的 ip 地址,IPv4 等。添加一个新行,如 190.123.123.3 Yourterminalname.local 它对我有用

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 本地主机

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

I was having the same issue.我遇到了同样的问题。

macOS Catalina macOS Catalina

Version: 10.15.7版本:10.15.7

在此处输入图片说明

The solution was::解决方案是::

import socket

if env == "PRODUCTION":
    ip_address = socket.gethostbyname(socket.gethostname())
else:
    ip_address = ""

I had the same error, so I used the subprocess command line utility to find the ip address of my machine.我有同样的错误,所以我使用 subprocess 命令行实用程序找到我机器的 ip 地址。 The code I used was:我使用的代码是:

    from sys import platform
    if platform == "linux" or platform == "linux2":
        # linux
        ip = sb.run(["ipconfig", "getifaddr", "en0"], capture_output=True)
        ...
    elif platform == "darwin":
        # OS X
        ip = sb.run(["ipconfig", "getifaddr", "en0"], capture_output=True)
        ...
    elif platform == "win32":
        # Windows...
        ip = sb.run(["ipconfig", "| findstr /i" "ipv4"],capture_output=True)
        ...
    return ip.stdout.decode()

It returns the IPv4 address of my machine and works on Windows, Mac and Linux它返回我机器的 IPv4 地址并在 Windows、Mac 和 Linux 上工作

If 127.0.0.1 localhost already exists in your /etc/hosts, try to restart MacBook.如果 127.0.0.1 localhost 已存在于您的 /etc/hosts 中,请尝试重新启动 MacBook。 It helps to me.这对我有帮助。

Kind of obvious but if you cloned a project that is using an env variable for your port... best to make sure youve actually set up those env variables...有点明显,但是如果您克隆了一个为您的端口使用 env 变量的项目......最好确保你已经实际设置了这些 env 变量......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gaierror:[Errno 8]提供的节点名或服务名,或者未知 - gaierror: [Errno 8] nodename nor servname provided, or not known gaierror: [Errno 8] 节点名或服务名已提供,或未知 - gaierror: [Errno 8] nodename nor servname provided, or not known 正在获取socket.gaierror:[Errno 8]节点名或服务名已提供,或者未知 - Getting socket.gaierror: [Errno 8] nodename nor servname provided,or not known Tweepy:[Errno 8]提供nodename或servname,或者不知道 - Tweepy: [Errno 8] nodename nor servname provided, or not known PySpark:[Errno 8] nodename 或 servname 提供,或未知 - PySpark: [Errno 8] nodename nor servname provided, or not known WSGIServerException:[Errno 8]提供了nodename或servname,或者未知 - WSGIServerException: [Errno 8] nodename nor servname provided, or not known python3 telnet socket.gaierror:[错误号8]提供的节点名或服务名,或者未知 - python3 telnet socket.gaierror: [Errno 8] nodename nor servname provided, or not known Paramiko SSH 连接错误:socket.gaierror: [Errno 8] nodename 或 servname 提供,或未知 - Paramiko SSH connection error: socket.gaierror: [Errno 8] nodename nor servname provided, or not known Flask gRPC betterproto socket.gaierror: [Errno 8] nodename nor servname provided, or not known - Flask gRPC betterproto socket.gaierror: [Errno 8] nodename nor servname provided, or not known socket.gaierror: [Errno 8] nodename or servname provided, or not known: Dash App - socket.gaierror: [Errno 8] nodename nor servname provided, or not known : Dash App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM