Python 调用getPage时Twisted中出现异常
我正在打电话给client.getPage,收到了以下回溯。有人能理解这一点吗Python 调用getPage时Twisted中出现异常,python,twisted,twisted.client,Python,Twisted,Twisted.client,我正在打电话给client.getPage,收到了以下回溯。有人能理解这一点吗 Traceback (most recent call last): File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/python/log.py", line 84, in callWithLogger return callWithContext({"system": lp}
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/python/context.py", line 59, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/python/context.py", line 37, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/internet/tcp.py", line 664, in doConnect
self._connectDone()
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/internet/ssl.py", line 160, in _connectDone
self.startTLS(self.ctxFactory)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/internet/tcp.py", line 561, in startTLS
if Connection.startTLS(self, ctx, client):
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.0.0-py2.6-linux-i686.egg/twisted/internet/tcp.py", line 402, in startTLS
self.socket = SSL.Connection(ctx.getContext(), self.socket)
exceptions.AttributeError: 'str' object has no attribute 'getContext'
[Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectError'>: An error occurred while connecting: [Failure instance: Traceback (failure with no frames): <type 'exceptions.AttributeError'>: 'str' object has no attribute 'getContext'
].
]
campfire.py:
import base64
from twisted.web import client
class Campfire(object):
"""Holds all methods to communicate to the campfire server."""
_resource = {'showAuthUser': '/users/me.xml'}
def __init__(self, subdomain, username, password):
"""Initialize campfire object.
Arguments:
subdomain -- campfire subdomain
username -- campfire username
password -- campfire password
"""
self.subdomain = subdomain
self.username = username
self.password = password
self.uri = 'https://' + subdomain + '.campfirenow.com'
def showAuthUser(self):
"""Make a request to the campfire server.
Returns a getPage object which is deferred.
"""
u = self.uri + self._resource['showAuthUser']
m = 'GET'
n = self.username
p = self.password
b = base64.encodestring('{0}:{1}'.format(n, p))
h = {'Authorization': 'Basic ' + b.strip()}
return self._getPage(u, m, h)
def _getPage(self, url, method, headers=None):
"""Extends Twisted's getPage method for use within the Campfire
object.
Returns a deferred and message.
Arguments:
url -- url to server
method -- request method
Keyword Arguments:
headers -- requested headers (default None)
"""
if headers:
return client.getPage(url, method, headers=headers)
您以错误的方式调用web.client.getPage。第二个参数应该是contextFactory,但您提供了一个字符串:
def getPage(url, contextFactory=None, *args, **kwargs)
将最后一行修改为如下所示:
return client.getPage(url, contextFactory=None, method, headers=headers)
(您可以跳过contextFactory=,但我认为这更清楚)没有一行代码吗?随机猜测:您传递了多个参数,但忽略了第二个参数应该是contextFactory/None?def getPage(url,contextFactory=None,*args,**kwargs)呵呵。很好的猜测,我想可能就是这样。因为我以str的形式传入了方法。我想我应该以kwarg的形式传入它。谢谢“会测试的。”汤玛萨88就是这样!谢谢
return client.getPage(url, contextFactory=None, method, headers=headers)