跳至内容
Python 俱乐部
用户工具
登录
站点工具
搜索
工具
显示页面
修订记录
反向链接
最近更改
媒体管理器
网站地图
登录
>
最近更改
媒体管理器
网站地图
您的足迹:
python-network-application:get-ip-address
本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。
====== 使用Python获得本机IP地址 ====== 使用Python可以用很简单的方法得到本机IP地址,不过在Windows和Linux下的方法稍有不一样的, ===== Windows下获得IP地址的方法 ===== ==== 方法一 ==== 使用拨号上网的话,一般都有一个本地ip和一个外网ip,使用python可以很容易的得到这两个ip 使用gethostbyname和gethostbyname_ex两个函数可以实现 <code python> import socket localIP = socket.gethostbyname(socket.gethostname())#得到本地ip print "local ip:%s "%localIP ipList = socket.gethostbyname_ex(socket.gethostname()) for i in ipList: if i != localIP: print "external IP:%s"%i </code> ==== 方法二 ==== <code python> import socket myname = socket.getfqdn(socket.gethostname()) myaddr = socket.gethostbyname(myname) </code> ===== Linux下获得IP地址的方法 ===== 上面的方法在Linux下也可以使用,除此之外,Linux下还可以用下面的方法得到本机IP地址。 Uses the Linux SIOCGIFADDR ioctl to find the IP address associated with a network interface, given the name of that interface, e.g. "eth0". The address is returned as a string containing a dotted quad. <code python> import socket import fcntl import struct def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24]) </code>
python-network-application/get-ip-address.txt
· 最后更改: 2011/07/22 23:50 (外部编辑)
页面工具
显示页面
修订记录
反向链接
回到顶部