欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取本电脑外网IP和内网IP

程序员文章站 2022-06-03 17:50:26
...
# -*- coding:utf-8 -*-
import requests, json, socket

"""获取外网IP"""
my_ip = requests.get('http://ip.42.pl/raw').text
print("11",my_ip)


my_ip = json.loads(requests.get('http://jsonip.com').text)['ip']
print("22",my_ip)


my_ip = json.loads(requests.get('https://api.ipify.org/?format=json').text)['ip']
print("33",my_ip)

"""获取内网IP"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 88))
ip = s.getsockname()[0]
print(ip)

 

相关标签: IP