执行socket.gethostbyname(socket.gethostname()) 报 socket.gaierror: [Errno -2] Name or service not known 错误。分析记录如下:
更正前
执行命令vi /etc/hosts:
![](https://img-blog.csdnimg.cn/2e39a0fcceee4d5db47651a1c87b50ca.png)
执行命令:vi /etc/hostname:
![](https://img-blog.csdnimg.cn/c647c065675e4e07b8200b1e93ed1ef9.png)
更正后
执行命令vi /etc/hosts:
![](https://img-blog.csdnimg.cn/3c38af2a7f304d6fa65b0830c10ad5f7.png)
执行命令:vi /etc/hostname:
![](https://img-blog.csdnimg.cn/c647c065675e4e07b8200b1e93ed1ef9.png)
验证
![](https://img-blog.csdnimg.cn/40384fd0d3f243a5869882b991ff6ebb.png)
总结
这个错误是由 /etc/hostname里的hostname 和 /etc/hosts里的 IP hostname 值不一致造成的
参考链接:
https://www.cnblogs.com/123rongzhi/p/16140849.html