获取局域网ip
import socket
def get_host_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80)) # 114.114.114.114也是dns地址
ip = s.getsockname()[0]
finally:
s.close()
return ip
print("本机局域网IP是:", get_host_ip())
获取公网ip
from urllib.request import urlopen
ip = urlopen('http://ip.42.pl/raw').read().decode()
print('本机所在公网IP是:', ip)
标签:socket,ip,公网,局域网,获取,8.8
From: https://www.cnblogs.com/chunyouqudongwuyuan/p/16914235.html