自动获取本地公网与内网IP
import json
import socket
import requests
def get_host_Kip():
try:
ip = requests.get('https://whois.pconline.com.cn/ipJson.jsp?json=true&ip=')
json_data = json.loads(ip.text)
except:
json_data = {'ip': 1}
return json_data['ip']
print('本机的公网IP是:', get_host_Kip())
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())