Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sockets

socket.gethostbyname(socket.gethostname()) worked well on OS X El Capitan. However, it's not working now after the Mac updated to 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
like image 553
FBL Avatar asked Oct 11 '16 04:10

FBL


2 Answers

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.

Alternatively, you can use socket.gethostbyname('localhost'), assuming that localhost is what you need and it is defined in your /etc/hosts.

like image 77
Armut Avatar answered Oct 09 '22 09:10

Armut


Same problem tome. I change the code to:

import socket
socket.gethostbyname("")

And it works now.

like image 16
jxc Avatar answered Oct 09 '22 10:10

jxc