将 CURL 命令转换为 python 用于从 Cloudflare API 获取数据的代码
Translate CURL command to python code for fetching data from Cloudflare API
我尝试创建 python 代码来获取 CloudFlare 分析仪表板 的 api 数据。这就是我将以下 Curl 命令翻译成 Python 的方式:
我想查询持续时间为1天的数据(从 2018-11-18 00:00:00 直到 2018 -11-19 00:00:00).
卷曲命令:
curl -X GET "https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true" \
-H "Content-Type:application/json" \
-H "X-Auth-Key:1234567893feefc5f0q5000bfo0c38d90bbeb" \
-H "X-Auth-Email:example@example.com"
Python代码:
import http.client, json
def find_data():
conn = http.client.HTTPSConnection("api.cloudflare.com")
headers = {
'Content-Type': "application/json",
'X-Auth-Email': "example@example.com",
'X-Auth-Key': "1234567893feefc5f0q5000bfo0c38d90bbeb",
}
payload = "since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true"
conn.request("POST", "/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard", payload, headers)
res = conn.getresponse()
data = res.read().decode('utf-8')
loadedjson = json.loads(data)
return(loadedjson)
print(find_data())
问题:
脚本正在运行,但我正在获取从 11 月 17 日到当前日期的数据,即我没有从 11 月 18-19 日但 17-24 获取查询数据。
这是查询云耀斑数据的正确方法吗,如果不正确请更正。如果 python 中有其他方法可以从 API 中获取数据,请修改脚本。
import requests
headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}
response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)
我尝试创建 python 代码来获取 CloudFlare 分析仪表板 的 api 数据。这就是我将以下 Curl 命令翻译成 Python 的方式:
我想查询持续时间为1天的数据(从 2018-11-18 00:00:00 直到 2018 -11-19 00:00:00).
卷曲命令:
curl -X GET "https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true" \
-H "Content-Type:application/json" \
-H "X-Auth-Key:1234567893feefc5f0q5000bfo0c38d90bbeb" \
-H "X-Auth-Email:example@example.com"
Python代码:
import http.client, json
def find_data():
conn = http.client.HTTPSConnection("api.cloudflare.com")
headers = {
'Content-Type': "application/json",
'X-Auth-Email': "example@example.com",
'X-Auth-Key': "1234567893feefc5f0q5000bfo0c38d90bbeb",
}
payload = "since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true"
conn.request("POST", "/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard", payload, headers)
res = conn.getresponse()
data = res.read().decode('utf-8')
loadedjson = json.loads(data)
return(loadedjson)
print(find_data())
问题:
脚本正在运行,但我正在获取从 11 月 17 日到当前日期的数据,即我没有从 11 月 18-19 日但 17-24 获取查询数据。
这是查询云耀斑数据的正确方法吗,如果不正确请更正。如果 python 中有其他方法可以从 API 中获取数据,请修改脚本。
import requests
headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}
response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)