python获取内网IP和外网IP的方法

it2026-01-05  9

python获取内网IP和外网IP的方法

获取内网IP:

import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 80)) print(s.getsockname()[0]) s.close()

获取外网IP:

import requests print(requests.get('http://ifconfig.me/ip', timeout=1).text.strip())
最新回复(0)