【问题标题】:Getting response code with hvac使用 hvac 获取响应代码
【发布时间】: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() 函数来获取响应代码吗?

【问题讨论】:

    标签: python hashicorp-vault


    【解决方案1】:

    我快速浏览了 hvac 代码,您无权访问基础状态并且它没有保存。您可以子类化一个新的 hvac 模块并稍微修改它。但是,你的问题是你能不能只修改单个函数。虽然我不会这样做,但我认为它回答了这个问题:

    result = client._get('/v1/secret/prometheus').status_code
    

    或者,你可以这样做:

    result = client._get('/v1/secret/prometheus')
    result.json() # has the json return value and
    result.status_code # this is the number you are looking for
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 2011-03-02
      • 2018-02-27
      • 1970-01-01
      相关资源
      最近更新 更多