1. 首页
  2. 数据库
  3. 其它
  4. python 获取本机ip地址的两个方法

python 获取本机ip地址的两个方法

上传者: 2020-12-30 23:41:27上传 PDF文件 26.3KB 热度 11次
第一种:复制代码 代码如下: 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]) #get_ip_address(‘lo’)环回地址 #get_ip_address(‘eth0’)
下载地址
用户评论