调用 getPage 时 Twisted 中出现异常
我正在调用 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}, 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'
].
]
main.py:
from twisted.internet import reactor
import campfire
def response(r):
print r
reactor.stop()
def error(e):
print e
reactor.stop()
c = campfire.Campfire('',
'',
''
)
d = c.showAuthUser()
d.addCallbacks(response, error)
reactor.run()
营火.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)
I'm calling client.getPage and I receive the following traceback. Can anyone make any sense of this?
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'
].
]
main.py:
from twisted.internet import reactor
import campfire
def response(r):
print r
reactor.stop()
def error(e):
print e
reactor.stop()
c = campfire.Campfire('',
'',
''
)
d = c.showAuthUser()
d.addCallbacks(response, error)
reactor.run()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您以错误的方式调用 web.client.getPage 。第二个参数应该是 contextFactory 但你提供一个字符串:
修改你的最后一行,使其看起来像这样:(
你可以跳过 contextFactory=,但我认为它更清楚)
You are calling web.client.getPage in the wrong way. The second argument should be a contextFactory but you provide a string:
Modify your last line to look like this:
(You can skip contextFactory=, but I think it is more clear)