使用python的socket模块查找ip地址 [英] Finding ip address using python's socket module

查看:63
本文介绍了使用python的socket模块查找ip地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下命令获取主机ip时:

When I run the following command to get the host ip:

socket.gethostbyname(socket.gethostname())

我在 MAC 终端上收到以下错误:

I am getting the following error on MAC terminal:

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

知道可能是什么问题吗?

Any idea what the problem could be?

socket.gethostname() 确实返回了正确的主机名.

socket.gethostname() does return the correct hostname.

推荐答案

尝试以root身份打开/etc/hosts文件,并添加如下一行

Try to open as root /etc/hosts file, and add a line like the following

# IP ADDR      HOSTNAME
192.168.1.1    your_desired_hostname

并且名称应该解析.

显然将 192.168.1.1 替换为您的 IP 地址.

Obviously substitute 192.168.1.1 with your ip address.

这篇关于使用python的socket模块查找ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆