如何使用 python 找到我的 ip 地址而不是本地 ip ? 我尝试使用套接字,但它找到本地 ip。我需要以太网外部 ip。这是我的代码,但它找到本地 ip
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
最佳答案
我曾经遇到过这个问题,这是我的解决方案:
您应该安装此软件包:
import requests
...
public_ip = requests.get("http://wtfismyip.com/text").text
print(public_ip)
使用socket无法获取公网IP
关于python - 我怎样才能用python找到我的IP地址而不是本地IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61347442/