Search code examples
pythontwisted

Twisted Agent won't build request from bytes


I'm learning Twisted by trying to build an RSS agregator. When I try to build requests using the web agent, I'm told that I did not provide the url argument as bytes :

[Failure instance: Traceback (failure with no frames): <class 'twisted.web._newclient.RequestGenerationFailed'>: [<twisted.python.failure.Failure builtins.TypeError: sequence item 0: expected a bytes-like object, str found>]

But I think I did :

from twisted.internet import reactor
from twisted.web.client import Agent

def request_sent(response):
    print ('I got something!')

def request_failed(reason):
    print (reason)

def feed_loader_main():
    """
    Starts and manage the reactor
    """
    agent = Agent(reactor)

    d = agent.request(
        'GET',
        'http://www.example.com'.encode('utf8')   ##### <- HERE
    )

    d.addCallback(request_sent)
    d.addErrback(request_failed)

    print ('Firing reactor!')
    reactor.run()

if __name__ == '__main__':
    feed_loader_main()

Is it Twisted black magic going on here or just poor encoding from me?


Solution

  • The exception didn't actually say that you didn't provide the URL as bytes. It just said somewhere it wanted bytes and got str (unicode) instead.

    I'm guessing you're on Python 3 since I can replicate your exception with your code on Python 3 and not on Python 2. I'm not sure what version of Twisted you're using but I suspect this isn't terribly Twisted-version-specific. Nevertheless, it's a good idea to specify the versions of Python and Twisted in future questions.

    The other value you're passing in to request is "GET" and on Python 3, that's a str (unicode). If you encode that (or just make it a bytes literal with b"...") then the exception goes away.