'NoneType'对象在scrapy \ twisted \ openssl中没有属性'_app_data'


问题内容

在使用scrapy的抓取过程中,我的日志中会不时出现一个错误。它似乎不在我的代码中的任何地方,并且看起来像是twisted \
openssl中的某个东西。任何想法是什么原因造成的,以及如何消除它?

这里的堆栈跟踪:

[Launcher,27487/stderr] Error during info_callback
    Traceback (most recent call last):
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/protocols/tls.py", line 415, in dataReceived
        self._write(bytes)
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/protocols/tls.py", line 554, in _write
        sent = self._tlsConnection.send(toSend)
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1270, in send
        result = _lib.SSL_write(self._ssl, buf, len(buf))
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 926, in wrapper
        callback(Connection._reverse_mapping[ssl], where, return_code)
    --- <exception caught here> ---
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/internet/_sslverify.py", line 1055, in infoCallback
        return wrapped(connection, where, ret)
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/twisted/internet/_sslverify.py", line 1157, in _identityVerifyingInfoCallback
        transport = connection.get_app_data()
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1589, in get_app_data
        return self._app_data
      File "/opt/webapps/link_crawler/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1148, in __getattr__
        return getattr(self._socket, name)
    exceptions.AttributeError: 'NoneType' object has no attribute '_app_data'

问题答案:

乍一看,这似乎是由于scrapy错误所致。Scrapy定义了自己的Twisted“上下文工厂”:https
:
//github.com/scrapy/scrapy/blob/ad36de4e6278cf635509a1ade30cca9a506da682/scrapy/core/downloader/contextfactory.py#L21-L28

该代码ClientTLSOptions使用其打算返回的上下文实例化。实例化此类的副作用是在上下文工厂上安装了“信息回调”。信息回调要求Twisted
TLS实现已在连接上设置为“应用程序数据”。但是,由于没有人使用该ClientTLSOptions实例(实例将立即丢弃),因此永远不会设置应用程序数据。

当信息回调返回以获取Twisted TLS实现(必须完成其一部分工作)时,它会发现没有应用程序数据,并会因您报告的异常而失败。

的副作用ClientTLSOptions有点令人不愉快,但我认为这显然是由于滥用/滥用造成的刮虫ClientTLSOptions。我认为此代码不可能经过很好的测试,因为每次证书未能验证时都会发生此错误。

我建议将错误报告给Scrapy。希望他们能解决对它的使用ClientTLSOptions并为您消除此错误。