美文网首页
host = socket.gethostname()错误

host = socket.gethostname()错误

作者: 放风筝的小小马 | 来源:发表于2018-07-26 15:45 被阅读272次

    调用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
    

    相关文章

      网友评论

          本文标题:host = socket.gethostname()错误

          本文链接:https://www.haomeiwen.com/subject/sitbmftx.html