Question

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)
Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top