import socket,fcntl,struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', (ifname + '\0').encode('utf-8'))
)[20:24])
# a,b,c,d = ip.split('.')
return ip
def get_ips():
try:
ip=get_ip_address('wlan0')
print(ip)
return ip
except :
ip=get_ip_address('wlan1')
print(ip)
return ip
ip=get_ips()
print(ip)
python get ip
最新推荐文章于 2024-04-26 17:29:05 发布