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
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
.
Same problem tome. I change the code to:
import socket
socket.gethostbyname("")
And it works now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With