我一直在尝试在 python 中获取实时 IP 地址并使用代理服务器更改它。我看到的代码是针对本地IP而不是公共IP。
如何使用python获取公共IP?即我从 whatismyip 获得的 IP,并且可以在 Windows 上使用 Python 中的代理请求更改公共 IP。
您可以使用多种服务来获取您的 IP。我用得最多的是
ipinfo.io/ip
您可以使用以下代码
import requests
proxies = {
'http': 'http://<IP>:<PORT>',
'https': 'http://<IP>:<PORT>',
}
req = requests.get('http://ipinfo.io/ip', proxies=proxies)
print (req.text)
import hek
# get ip
ip = hek.ipstuff.myip(find="query")
# print ip
print(ip)
您还可以使用 ident 检索您的公共 IP:
import requests
response = requests.get('https://ident.me')
if response.status_code == 200:
print(f"IP >>> {resp.content.decode('utf8')}")