这篇文章主要介绍了来源gao($daima.com搞@代@#码网Python获取Linux系统下的本机IP地址代码分享,本文直接给出实现代码,可以获取到eth0等网卡的IP地址,需要的朋友可以参考下
有时候使用到获取本机IP,就采用以下方式进行。
代码如下:
#!/usr/bin/python
import socket
import struct
import fcntl
def getip(ethname):
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack(‘256s’, ethname[:15]))[20:24])
if __name__==’__main__’:
print getip(‘eth0’)
import socket
import struct
import fcntl
def getip(ethname):
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack(‘256s’, ethname[:15]))[20:24])
if __name__==’__main__’:
print getip(‘eth0’)
以上就是Python获取Linux系统下的本机IP地址代码分享的详细内容,更多请关注gaodaima搞代码网其它相关文章!