【发布时间】:2018-06-13 10:17:21
【问题描述】:
我正在尝试找到一种方法来获取我进行的 API 调用的响应代码。
我正在连接到保管库服务器并尝试获取用户名和密码。 但我的要求发生了变化,现在我只想监控对我的 API 调用的 HTTP 响应。
我的代码是:
#/usr/bin/python
import os
import hvac
import requests
def establishConnection(crt,key):
client = hvac.Client(url='https://127.0.0.1:8200',
verify=False,
cert=(crt, key))
try:
client.auth_tls()
except (ConnectionRefusedError, requests.exceptions.ConnectionError, requests.packages.urllib3.exceptions.ProtocolError, hvac.exceptions.VaultDown):
print("establishConnection() failed.")
return client
def apiCheck(crt, key):
client = establishConnection(crt, key)
result = client.read('secret/prometheus')['data']['password']
# In the above line, rather than having the value returned , I want the response code.
我尝试搜索多种解决方案,但似乎没有一个是特定于 hvac 的。
我可以使用requests.head 或urllib.request.urlopen(url).getcode(),但我必须为此编写一个完整的单独代码,而不是能够重用establishConnection(crt,key) 函数。
知道如何只修改apiCHeck() 函数来获取响应代码吗?
【问题讨论】: