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'
].
]

主要.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)
4

1 回答 1

2

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

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

修改你的最后一行看起来像这样:

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

(可以跳过contextFactory=,但我觉得更清楚)

于 2011-05-01T09:31:56.367 回答