我想弄清楚如何從API調用中獲取所有字典鍵,並將它們插入到平面文件中。python:將字典鍵打印到文件
#!/usr/bin/env python
import requests
import json
import time
import urllib3
from base64 import b64encode
requests.packages.urllib3.disable_warnings()
#
# GET /dashboards/{dashboardId}/widgets/{widgetId}/value
test_dashboard = "557750bee4b0033aa111a762"
test_widget = "8bad2fc0-5c9b-44f2-a54b-05c8c6f9552b"
apiserver = "http://serveraddress"
userpass = b64encode(b"myuser:mypass").decode("ascii")
headers = { 'Authorization' : 'Basic %s' % userpass }
def get_apicall(dashboardId, widgetId):
response = requests.get(
apiserver + "/dashboards/" +
dashboardId + "/widgets/" +
widgetId + "/value",
headers=headers,
verify=False)
json_data = json.loads(response.text)
print(json.dumps(json_data["result"]["terms"], indent=2))
get_apicall(test_dashboard, test_widget)
,其輸出是這樣的:
[[email protected] ]$ ./shunhosts.py
{
"71.6.216.39": 2,
"71.6.158.166": 2,
"71.6.216.55": 2,
"71.6.216.56": 2
}
我想代碼寫/追加每個字典鍵,新的生產線的純文本文件:即
71.6.216.39
71.6.158.166
71.6.216.55
71.6.216.56
有一個'keys()'方法 – tuxtimo