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)
#!/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)
相关文章推荐
- linux环境下获取真实的ip地址
- linux 接口IP地址获取
- linux和Qt下获取本机每个接口的ip地址
- Linux 接口地址的获取-暨获取本地IP地址(所有地址,包括IPv4,IPV6,MAC 地址)
- Linux 接口地址的获取-暨获取本地IP地址(所有地址,包括IPv4,IPV6,MAC 地址)
- linux下获取本机ip地址,ipv4和ipv6
- linux下获取系统环境变量
- linux 下获取本地IP地址
- Java 获取Linux 的IP地址
- Linux 获取本机IP地址和MAC地址(三)
- linux编程获取本机IP地址
- Unix/Linux环境C编程新手教程(22) C/C++怎样获取程序的执行时间
- Linux下Python获取IP地址
- LINUX下获取IP地址和MAC地址
- linux 下获取本地IP地址
- Linux通过命令仅获取IP地址的方法
- Java获取主机的网络接口和IP地址
- Linux环境下手动设置IP地址
- WCF接口获取请求的IP地址
- Linux 环境 C语言 操作MySql 的接口范例