Python获取本机IP地址
来源:互联网 发布:淘宝授权书怎么弄 编辑:程序博客网 时间:2024/06/06 19:39
使用Python可以用很简单的方法得到本机IP地址,不过在Windows和Linux下的方法稍有不一样的,
Windows下获得IP地址的方法
方法一
使用拨号上网的话,一般都有一个本地ip和一个外网ip,使用python可以很容易的得到这两个ip 使用gethostbyname和gethostbyname_ex两个函数可以实现
import socketlocalIP = socket.gethostbyname(socket.gethostname())#得到本地ipprint "local ip:%s "%localIP ipList = socket.gethostbyname_ex(socket.gethostname())for i in ipList: if i != localIP: print "external IP:%s"%i
方法二
import socket myname = socket.getfqdn(socket.gethostname())myaddr = socket.gethostbyname(myname)
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.
import socketimport fcntlimport 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])
- Python获取本机IP地址
- Python获取本机Ip地址
- python 获取linux 本机IP地址
- Python获取本机Ip地址
- Python 获取本机ip地址
- Python+socket获取本机IP地址
- 通过 python 获取本机IP地址
- Python获取本机所有IP地址
- Python获取本机所有IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- 获取本机IP地址
- Class.forName("com.mysql.jdbc.Driver");的作用
- LINK : fatal error L1093: xxx.obj : object file not found
- 黑马程序员——Servlet生命周期和Servlet之间的跳转
- oracle 11g 学习笔记 2012_10_24(1)
- NSKeyedArchiver用法
- Python获取本机IP地址
- 想起你,连呼吸都疼那么,你听得到祝福就好.
- 在网页title左侧,地址栏左侧,收藏夹地址加logo的实现
- MSSQLSERVER 数据库快照
- opengl学习(五)一个机械臂程序,有关模型转换
- Android模拟器抓包(TCPDUMP+Wireshark)
- android Notification
- Linux学习资源及准备工作
- C#中String和byte[]转换