首页 电脑 电脑学堂 查看内容

python获取网卡IP地址

2011-8-10 11:15 801 0

摘要: #!/usr/bin/env python  # -*- coding: utf-8 -*-    impor...
关键词: nbsp socket address import ifname struct get print fcntl INET

#!/usr/bin/env python  # -*- coding: utf-8 -*-    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])    print "eth0 = "+ get_ip_address('eth0')  print "lo = " + get_ip_address('lo') 
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除

路过

雷人

握手

鲜花

鸡蛋

最新评论

返回顶部