How to Solve Error: socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Situation 1:
Error content:
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Reason:
hostname is not written in /etc/hosts. For example, the MAC-20150101 in the abnormal information above is actually the host name of our Mac system. Some codes may need to find the corresponding IP address in the local DNS according to the host name, because the local DNS configuration is not specified What is the IP address of the host name, this error will also be prompted.
Solution:
Implement in python interpreter:
python
Call the gethostname() method provided by the Socket library to obtain the host name
>>>import socket >>>socket.gethostname() >>>>exit()
Get the host name, modify the hosts file, enter the command
vim /etc/hosts
Add the host name 127.0.0.1 hostname
Error resolution
Situation 2:
The solution to prompt nodename nor servname provided when the Java Web project is started on the Mac system
java.net.UnknownHostException: MAC-20150101: MAC-20150101: nodename nor servname provided, or not known at java.net.InetAddress.getLocalHost(InetAddress.java:1473) at org.eclipse.rse.core.RSECorePlugin.getLocalMachineName(RSECorePlugin.java:265) at org.eclipse.rse.core.RSEPreferencesManager.getDefaultPrivateSystemProfileName(RSEPreferencesManager.java:358) at org.eclipse.rse.core.RSEPreferencesManager.initDefaults(RSEPreferencesManager.java:337) at org.eclipse.rse.internal.core.RSEPreferenceInitializer.initializeDefaultPreferences(RSEPreferenceInitializer.java:23) at org.eclipse.core.internal.preferences.PreferenceServiceRegistryHelper$1.run(PreferenceServiceRegistryHelper.java:300) at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) .... .... .... Caused by: java.net.UnknownHostException: MAC-20150101: nodename nor servname provided, or not known at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293) at java.net.InetAddress.getLocalHost(InetAddress.java:1469) ... 28 more
Regarding the MAC-20150101 in the exception information, it is actually the host name of our Mac system. Some codes may need to find the corresponding IP address in the local DNS according to the host name, because the local DNS configuration does not specify the host What is the name of this IP address, this error will also be prompted.
The solution is very simple:
1) Open the terminal on the Mac system, check the current host DNS configuration, enter the command cat /private/etc/hosts, as shown in the figure:
2) Then edit the host configuration, add the mapping of the host name, enter the command sudo vi /private/etc/hosts, enter the VI editor, and add the following mapping
127.0.0.1 MAC-20150101
Just exit and save.
3) Finally, enter the refresh command dscacheutil -flushcache in the terminal
After three steps, when starting the Java Web project, the following error message will not appear.
The reason for this error is that the Internet says that there is a place in the project that calls the following code:
InetAddress.getLocalHost().getCanonicalHostName();
It is said that this method will return FQDN (Fully Qualified Domain Name), if the host name is not configured, then calling this code will throw an exception message, and this method depends on the underlying operating system, the configuration of the Mac system is somewhat different from that of Windows !
Situation 3:
Socket.gaierror: [Errno -2] Name or service not known error solution when configuring the remote server Jupyter notebook
Modify the jupyter_notebook_config.py configuration file and enter the command:
vim ~/.jupyter/jupyter_notebook_config.py
In the configuration file, search for c.NotebookApp.ip, find this sentence and modify it to c.NotebookApp.ip=’0.0.0.0′ (general tutorial will write and modify it to’*’), save and restart the notebook.
## The IP address the notebook server will listen on. c.NotebookApp.ip = '*' c.NotebookApp.allow_remote_access=True