【问题标题】:Testrail Python client - SSL cert errorTestrail Python 客户端 - SSL 证书错误
【发布时间】:2018-04-18 10:46:30
【问题描述】:

我正在我的项目中为 Testrail 实现 Python 客户端 (http://docs.gurock.com/testrail-api2/bindings-python)

我正在运行 API 调用“get_test”并收到如下错误

File "playground.py", line 10, in <module>
    case = client.send_get('get_test/53948')
  File "/Users/bhdev/Work/Python/TBH/testrail.py", line 36, in send_get
    return self.__send_request('GET', uri, None)
  File "/Users/bhdev/Work/Python/TBH/testrail.py", line 70, in __send_request
    response = urllib.request.urlopen(request).read()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1320, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)>

我的代码

from testrail import *


client = APIClient('https://******')
client.user = '*****'
client.password = '******'

case = client.send_get('get_test/xxxx')

print(case)

如何绕过 SSL 证书问题。

谢谢

【问题讨论】:

  • 试试client.send_get('get_test/xxxx', verify=False)
  • 已经尝试过了,但服务器无法将验证识别为有效参数。我切换到请求并针对 testrail 形成自己的 API 请求
  • 您不能向client.send_get() 添加额外的参数,因为它只接受一个参数 - uri。在内部,这会调用__send_request()。请参阅下面的完整答案。

标签: python-3.x api testrail


【解决方案1】:

您的错误的根本原因是您运行请求的机器不信任您的 TestRail 服务器正在使用的证书。

您可以:

  • 修复 TestRail 服务器上的 SSL 证书
  • 根据PEP 476 :: Opting Out 中的说明修改绑定 (testrail.py) 以禁用证书验证

基于the current python bindings source code on github,您应该能够禁用证书验证,如下所示:

  • import json, base64(第14行)更改为import json, base64, ssl
  • 将以下行添加到__init__(self, base_url) 方法

self.context = ssl._create_unverified_context()

  • 修改以下行:

response = urllib.request.urlopen(request).read()

传递未经验证的上下文。

response = urllib.request.urlopen(request, context=self.context).read()

【讨论】:

    【解决方案2】:

    这个添加解决了我的问题:

    import ssl
    ssl._create_default_https_context = ssl._create_unverified_context

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 2015-10-20
      • 2013-08-04
      • 2010-10-16
      • 2017-04-28
      • 2012-02-15
      相关资源
      最近更新 更多