Search code examples
pythontwistedtwisted.web

Python Twisted framework HTTP client


I want to write a simple SSL HTTP client in Python and have heard about the Twisted framework.

I need to be able to authenticate with a REST service - so I was thinking I'd just POST a user name and password to the target server. Assuming authentication is successful, the client will receive a cookie.

Will an HTTP client built on Twisted automatically resend the cookie header for each subsequent request, or do I need to do something special? I've never used Twisted before.

Thanks


Solution

  • Will an HTTP client built on Twisted automatically resend the cookie header for each subsequent request, or do I need to do something special?

    "an HTTP client built on Twisted" will do anything it is built to do - just like, presumably any X built on any Y will do whatever it was built to do. :) So I might suggest that this isn't the question you really care about the answer to.

    Since Twisted 11.1.0, twisted.web.client.CookieAgent accepts a cookieJar argument which does two things:

    • it defines the cookies which are available to be sent with the requests
    • it stores new cookies received from servers in responses

    The soon-to-be-deprecated twisted.web.client.getPage accepts a cookies argument behaves similarly.

    So if you use CookieAgent then the cookie will be persisted and sent with subsequent requests, providing the authentication behavior you're after.

    You could also do something with getPage but given its impending doom you probably shouldn't.