当前位置: 代码迷 >> 综合 >> python3 根据网卡名获取IP
  详细解决方案

python3 根据网卡名获取IP

热度:88   发布时间:2024-02-23 21:24:24.0

如何根据网卡名获取到IP

import socket,struct,fcntldef get_ip(ifname):s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', bytes(ifname[:15],'utf-8')))[20:24])print("server ip:",get_ip('eth0'))