shell获取IP太麻烦,python实现还是快点
获取本机IP
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)
- 方法二
import socket
hostname=socket.gethostname()
ip=socket.gethostbyname(hostname)
print(ip)
转载于:https://blog.51cto.com/4489712/2318784