如何使用Python或带有Cloudflare的DDNS检查公共IP

如何使用Python或带有Cloudflare的DDNS检查公共IP,python,python-3.x,ubuntu,Python,Python 3.x,Ubuntu,有没有办法用Python检查我的公共IP地址?我在家里有一个关于Cloudflare和VPS的帐户(但动态IP)。我需要在OVH上有帐户之前更新VPN IP,迁移后DDNS工作不起作用。请求: import requests response = requests.get('http://ifconfig.me') print(response.text) python 2: import urllib2 response = urllib2.urlopen('http://ifconfig.

有没有办法用Python检查我的公共IP地址?我在家里有一个关于Cloudflare和VPS的帐户(但动态IP)。我需要在OVH上有帐户之前更新VPN IP,迁移后DDNS工作不起作用。

请求:

import requests
response = requests.get('http://ifconfig.me')
print(response.text)
python 2:

import urllib2
response = urllib2.urlopen('http://ifconfig.me')
print response.read()
python 3:

from urllib import request
response = request.urlopen('http://ifconfig.me')
print(response.read())

网上有很多网站提供简单的API来检查你连接的IP地址

例如:

➜ ~ curl 'https://api.ipify.org?format=json'
{"ip":"1.2.3.4"}
可以使用python库(如)在python代码中调用API

# download requests with `pip install requests`

import requests
res = requests.get("https://api.ipify.org?format=json")

your_ip = res.json() # {"ip" : "1.2.3.4"}


最近,我创建了一个简单的docker映像,允许您将本地ip与Cloudflare保持同步,也称为Cloudflare动态DNS(DDNS) 在这里查看: