這里也有一篇文章:Python獲取本機(jī)IP地址的方法(圖文) 相對(duì)簡(jiǎn)單了點(diǎn)。
方法1:
-
-
- 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,
- struct.pack('256s', ifname[:15])
- )[20:24])
-
-
方法2:
-
-
- def get_local_ip(ifname):
- import socket, fcntl, struct
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
- ret = socket.inet_ntoa(inet[20:24])
- return ret
- print get_local_ip("eth0")
本文原始鏈接:
http://www./article/6024.html更多有關(guān)python的內(nèi)容,可以參考python 教程系列文章。