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

Ald*_*und 10 python openssl twisted pyopenssl scrapy

在使用scrapy的抓取过程中,我的日志中会不时出现一个错误.它似乎不在我的代码中的任何地方,看起来像twisted\openssl里面的东西.是什么原因造成了这个以及如何摆脱它?

Stacktrace在这里:

[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'
Run Code Online (Sandbox Code Playgroud)

gec*_*eca 21

我能够通过安装service_identity包来解决这个问题:

pip install service_identity

  • 解决了这个问题.谢谢. (5认同)
  • 起初这对我不起作用,但通过强制卸载起作用!所以只需要执行`pip uninstall service_identity && pip install service_identity` (2认同)

Jea*_*one 5

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

此代码ClientTLSOptions使用它要返回的上下文进行实例化.实例化此类的副作用是在上下文工厂中安装了"info callback".信息回调要求Twisted TLS实现已在连接上设置为"app data".但是,由于什么都没有使用该ClientTLSOptions实例(它立即被丢弃),因此永远不会设置应用程序数据.

当回调信息回调以获得Twisted TLS实现(执行其部分工作所必需)时,它会发现没有应用程序数据,并且会因您报告的异常而失败.

副作用ClientTLSOptions有点令人不快,但我认为这显然是滥用/滥用造成的scrapy bug ClientTLSOptions.我不认为这个代码可能已经过很好的测试,因为每次证书无法验证时都会发生此错误.

我建议向Scrapy报告错误.希望他们可以修复他们的使用ClientTLSOptions并为您消除此错误.