utils_get_public_ip.py 437 B

123456789101112131415
  1. # -*- coding: utf-8 -*-
  2. import httpx
  3. def get_public_ip():
  4. try:
  5. # 使用 httpx 发起请求
  6. response = httpx.get("https://httpbin.org/ip", timeout=10)
  7. response.raise_for_status() # 检查请求是否成功
  8. ip_data = response.json()
  9. return ip_data["origin"]
  10. except httpx.RequestError as e:
  11. print(f"An error occurred while obtaining the public IP address.:{e}")
  12. exit(1)