用户工具

站点工具


程序设计:python:获取公网ip

这是本文档旧的修订版!


import requests

def get_public_ip():

  try:
      response = requests.get('https://httpbin.org/ip')
      response.raise_for_status()  # 检查是否请求成功
      ip = response.json()['origin']
      return ip
  except requests.RequestException as e:
      print(f"Error: {e}")
      return None

public_ip = get_public_ip() if public_ip:

  print(f"Your public IP is: {public_ip}")

else:

  print("Unable to get your public IP.")
程序设计/python/获取公网ip.1715306031.txt.gz · 最后更改: 2024/05/10 01:53 由 lnote