SSL.py TypeError: data must be a byte string

来源:互联网 发布:淘宝产品图价格 编辑:程序博客网 时间:2024/05/22 01:35

做爬虫已经一段时间了,不过一直抓的是http的网页,昨天突然有了一个https的需求,因为维基用http查敏感词会被封(你懂得),所以必须要用https.

不过抓的时候报了一个错,HTTPS not supported: install pyopenssl library

按照提示安装pyOpenSSL,可是安装后又报错

难道是Twisted有问题?按照twisted手册试试ssl协议,copy例子运行

from twisted.internet import ssl, reactor
from twisted.internet.protocol import ClientFactory, Protocol

class EchoClient(Protocol):
    def connectionMade(self):
        print "hello, world"
        ss="hello"
        self.transport.write(str.encode(ss))

    def dataReceived(self, data):
        print "Server said:", data
        self.transport.loseConnection()

class EchoClientFactory(ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()

if __name__ == '__main__':
    factory = EchoClientFactory()
    reactor.connectSSL('208.80.154.224', 80, factory, ssl.ClientContextFactory())
    reactor.run()

报了一样的错

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/python/log.py", line 84, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/python/log.py", line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/python/context.py", line 59, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/python/context.py", line 37, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/internet/tcp.py", line 177, in doWrite
    return Connection.doWrite(self)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/internet/tcp.py", line 428, in doWrite
    result = abstract.FileDescriptor.doWrite(self)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/internet/abstract.py", line 115, in doWrite
    l = self.writeSomeData(self.dataBuffer)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/internet/tcp.py", line 181, in writeSomeData
    return Connection.writeSomeData(self, data)
  File "/usr/local/lib/python2.7/site-packages/Twisted-10.0.0-py2.7-linux-x86_64.egg/twisted/internet/tcp.py", line 474, in writeSomeData
    return self.socket.send(buffer(data, 0, self.SEND_LIMIT))
  File "/usr/local/lib/python2.7/site-packages/OpenSSL/SSL.py", line 947, in send
    raise TypeError("data must be a byte string")
exceptions.TypeError: data must be a byte string

把Twisted升级为14.0.0版本,问题解决

原来是版本不兼容的原因

查看pyOpenSSL版本为0.14,经测试也可以将pyOpenssL将将为0.11也可运行。



0 0