您的位置:首页 > 运维架构 > Linux

linux环境获取接口IP地址

2017-09-20 10:24 429 查看
获取IP:

#!/usr/bin/env python

import socket

def Get_local_ip():

 """

 Returns the actual ip of thelocal machine.

 This code figures out whatsource address would be used if some traffic

 were to be sent out to somewell known address on the Internet. In this

 case, a Google DNS server isused, but the specific address does not

 matter much. No traffic isactually sent.

 """

 try:

  csock =socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

  #csock.connect(('8.8.8.8',80))

  csock.connect(('8.8.8.8',80))

  (addr, port) =csock.getsockname()

  csock.close()

  return addr

 except socket.error:

  return"127.0.0.1"

 

if __name__ == "__main__":

 local_IP = Get_local_ip()

 print(local_IP)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: