Home >Backend Development >Python Tutorial >Python获取Linux系统下的本机IP地址代码分享

Python获取Linux系统下的本机IP地址代码分享

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-10 15:19:071238browse

有时候使用到获取本机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')
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn