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
最新推荐文章于 2025-06-04 13:23:11 发布
本文介绍了如何使用Python的socket和fcntl库来获取网络接口(如wlan0或wlan1)的IP地址,通过SIOCGIFADDR系统调用来实现,并提供了一个get_ip_address和get_ips函数示例。
1万+

被折叠的 条评论
为什么被折叠?



