我有一個服務器串列:
hosts=['server1','server2','server3','server4']
有通過 api 呼叫訪問的服務器 4 監控工具。這些監控 env url 基本相同,只是每個 env 都有唯一的 id,它是 url 的一部分。這些服務器可以在 4 個監控工具中的任何一個上。我需要找出這些服務器屬于哪個 url。
例如,這些是監控工具的 url:
production_env="https://example.com/e/envid123"
dev_env="https://example.com/e/envid678"
test_env="https://example.com/e/envid567"
uat_env="https://example.com/e/envid1000"
給定服務器名稱,我需要找出它們屬于哪個環境。
給定服務器名稱,例如“server1”,
api url 將變為https://example.com/e/envid123&serverName="server1",此 url 將給出 server1 是否存在于 production_env 中。我需要檢查每個 env url,直到找到給定的服務器。
我正在嘗試這樣的事情:
envId=['envid123','envid678','envid567','envid1000']
for server in hosts:
for id in envId:
url="https://example.com/e/" id &serverName=server
resp=request.get(url)
有什么想法怎么能做到最好?
uj5u.com熱心網友回復:
您需要引號&serverName=將它們連接起來。但是使用格式化方法更簡單,例如 f-strings。
要查找所需的 URL,請使用resp.json獲取解碼的 JSON,并檢查相應字典元素的值。
found = False
for server in hosts:
for id in envId:
url = f"https://example.com/e/{id}&serverName={server}"
resp = request.get(url)
if resp.json['totalCount'] > 0:
found = True
print(f"Success at host = {server} id = {id}")
break
if found:
break
else:
print("No server found")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415106.html
標籤:
上一篇:如何從API連接中提取位元組型別的JSON資料,并將選定的列轉換為python資料框
下一篇:img-tag中src的JS邏輯
