首页 > 解决方案 > Airbrake-Django 引发 SSLError

问题描述

我正在尝试按照https://github.com/airbrake/airbrake-django#manually-sending-errors-to-airbrake上的示例手动向 Airbrake 发送错误消息。首先,我启动 Django shell:

(venv) Kurts-MacBook-Pro-2:lucy-web kurtpeek$ ENV_ROLE=staging python manage.py shell
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.conf import settings

环境使得AIRBRAKE设置被定义:

In [6]: settings.AIRBRAKE
Out[6]: 
{'API_KEY': '<airbrake_api_key>',
 'TIMEOUT': 5,
 'ENVIRONMENT': 'staging'}

但是,如果我尝试调用 AirbrakesClient.notify方法,我会得到SSLError

In [7]: from airbrake.utils.client import Client

In [8]: airbrake = Client()

In [9]: error = Exception("This is a test exception")

In [10]: airbrake.notify(error)
---------------------------------------------------------------------------
SSLError                                  Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1317                 h.request(req.get_method(), req.selector, req.data, headers,
-> 1318                           encode_chunked=req.has_header('Transfer-encoding'))
   1319             except OSError as err: # timeout error

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in send(self, data)
    963             if self.auto_open:
--> 964                 self.connect()
    965             else:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py in connect(self)
   1399             self.sock = self._context.wrap_socket(self.sock,
-> 1400                                                   server_hostname=server_hostname)
   1401             if not self._context.check_hostname and self._check_hostname:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)
    406                          server_hostname=server_hostname,
--> 407                          _context=self, _session=session)
    408 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in __init__(self, sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, family, type, proto, fileno, suppress_ragged_eofs, npn_protocols, ciphers, server_hostname, _context, _session)
    813                         raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--> 814                     self.do_handshake()
    815 

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in do_handshake(self, block)
   1067                 self.settimeout(None)
-> 1068             self._sslobj.do_handshake()
   1069         finally:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py in do_handshake(self)
    688         """Start the SSL/TLS handshake."""
--> 689         self._sslobj.do_handshake()
    690         if self.context.check_hostname:

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

Python requests SSL error - certificate verify failed看来,在这种情况下你需要一个证书,我想我的本地 Django shell 没有。可能无法以这种方式使用 Airbrake 吗?我将如何测试是否发送了手动添加的通知?

标签: pythondjangosslairbrake

解决方案


感谢您使用Airbrake

如果可以,请升级到本机支持Python 3.+的最新 Python 通知程序 pybrake

有关如何将pybrake与 Django 集成的说明位于此处

关于具体问题

SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

似乎 macOS 可能没有所需/更新的 CA 证书。

免责声明:我为 Airbrake 工作


推荐阅读