当前位置:首页 > Python教程 > python技巧

python 获取Linux IP

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])


print get_ip_address(‘eth0‘)


原文:http://961911.blog.51cto.com/951911/1547378


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐