标签:情况 shell 本机 close python实现 try finally 127.0.0.1 实现
shell获取IP太麻烦,python实现还是快点1.方法一 第一种更好用,第二种会出现127.0.0.1的情况
import socket
def get_host_ip():
try:
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect((‘114.114.114.114‘,80))
ip=s.getsockname()[0]
finally:
s.close()
return ip
ip=get_host_ip()
print(ip)
标签:情况 shell 本机 close python实现 try finally 127.0.0.1 实现
原文地址:http://blog.51cto.com/4489712/2318784