摘自于:https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python
1、利用外部的包
import netifaces as ni ni.ifaddresses('eth0') ip = ni.ifaddresses('eth0')[2][0]['addr'] print ip # should print "192.168.100.37"
2、不用外部的包
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('eth0') # '192.168.0.110'