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 10 12 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.
Same problem tome. I change the code to:
import socket
socket.gethostbyname("")And it works now.
2I had the same problem today (with MacOS Mojave) and was able to fix it using the link that @FBL provided in the comments.
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.
Rather than using the localhost, use the computer name as specified in /etc/hosts.
For example, /etc/host would have something like this:
127.0.0.1 ET02282-MAC.local
127.0.0.1 localhostAnd in your connection use "ET02282-MAC.local"
It seems like a python bug? You need to set the hostname & ipv4 , after this, you can fix it.
import socket
hostname = socket.gethostname()
print(hostname) >> samzong
# make sure do it
!echo "127.0.0.1 samzong" >> /private/etc/hosts Another simple solution is Go terminal paste sudo nano /etc/hosts find your ip adress that is needed , IPv4 etc. add a new line like 190.123.123.3 Yourterminalname.local it worked for me
I was having the same issue. I was not able to ping localhost.
ping localhost
>> error ping: cannot resolve localhost: Unknown hostI changed my hosts file.
sudo nano /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.
127.0.0.1 localhost
To confirm if everything works fine, try pinging your localhost
ping localhost
If you are using Jupyter notebook or not try this
IP4=str(ipaddress.ip_address(8888))
IPAddr = socket.gethostbyname(IP4)don't forget to import ipaddress & socket
I was having the same issue.
macOS Catalina
Version: 10.15.7
The solution was::
import socket
if env == "PRODUCTION": ip_address = socket.gethostbyname(socket.gethostname())
else: ip_address = "" If 127.0.0.1 localhost already exists in your /etc/hosts, try to restart 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... 😅
I had the same error, so I used the subprocess command line utility to find the ip address of my machine. The code I used was:
from sys import platform
def getIP(): 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