调用host = socket.gethostname()
后出现如下错误:
Traceback (most recent call last):
File "./02-server.py", line 9, in <module>
serverSocket.bind((hostname, socketPort))
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
是因为在本地hosts文件中无法获取到通过socket.gethostname()
得到的名称对应的ip地址,解决方法如下:
// 获取电脑的hostname
host = socket.gethostname(); //假如是myhostname.local;
// 编辑 /etc/hosts 文件,添加如下所示到hosts文件底部
127.0.0.1 myhostname.local
127.0.0.1 myhostname