调用 getPage 时 Twisted 中出现异常

发布于 2024-11-04 05:08:04 字数 3994 浏览 0 评论 0原文

我正在调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦等七年七年为一梦 2024-11-11 05:08:04

您以错误的方式调用 web.client.getPage 。第二个参数应该是 contextFactory 但你提供一个字符串:

def getPage(url, contextFactory=None, *args, **kwargs)

修改你的最后一行,使其看起来像这样:(

return client.getPage(url, contextFactory=None, method, headers=headers)

你可以跳过 contextFactory=,但我认为它更清楚)

You are calling web.client.getPage in the wrong way. The second argument should be a contextFactory but you provide a string:

def getPage(url, contextFactory=None, *args, **kwargs)

Modify your last line to look like this:

return client.getPage(url, contextFactory=None, method, headers=headers)

(You can skip contextFactory=, but I think it is more clear)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文