chris
chris

Reputation:

how to use session/cookie in twisted.web?

I am implementing an http server with twisted.web. Here comes the problem: there is a login operation; after that, I want the http server to remember each client using acookie/session, until the user closes the browser.

I've read the twisted.web document, but I can't figure out how to do this. I know the request object has a function named getSession(), then a session object will be returned. What next? How do I store the information during the several requests?

I also searched the twisted mail list; there nothing very helpful, and I am still confused. If someone used this before, please explain this to me, or even put some code here, so I can understand it myself. Thank you very much!

Upvotes: 9

Views: 4683

Answers (3)

matburt
matburt

Reputation: 66

Calling getSession() will generate a session and add the cookie to the request:

getSession() source code

If the clients already has a session cookie then calling getSession() will read it and return a Session with the original Session content. So it is transparent to your code whether or not it is actually creating the Session cookie or just reading it.

Session cookies have certain properties... if you want more control over the contents of the cookie then look at Request.addCookie() which getSession() calls behind the scenese.

Upvotes: 4

moshez
moshez

Reputation: 37565

You can use "request.getSession()" to get a componentized object.

You can read more about componentized in http://twistedmatrix.com/documents/current/api/twisted.python.components.Componentized.html -- the basic way of using it is via defining an interface and an implementation, and pushing your on objects into the session.

Upvotes: 4

Peter Gibson
Peter Gibson

Reputation: 19564

See this related question Store an instance of a connection - twisted.web. The answer there links to this blog post http://jcalderone.livejournal.com/53680.html, which shows an example of storing a counter for the number of visits for the session (thanks to jcalderone for the example):

# in a .rpy file launched with `twistd -n web --path .`
cache()

from zope.interface import Interface, Attribute, implements
from twisted.python.components import registerAdapter
from twisted.web.server import Session
from twisted.web.resource import Resource

class ICounter(Interface):
    value = Attribute("An int value which counts up once per page view.")

class Counter(object):
    implements(ICounter)
    def __init__(self, session):
        self.value = 0

registerAdapter(Counter, Session, ICounter)

class CounterResource(Resource):
    def render_GET(self, request):
        session = request.getSession()
        counter = ICounter(session)   
        counter.value += 1
        return "Visit #%d for you!" % (counter.value,)

resource = CounterResource()

Don't worry if this seems confusing - there are two things that you need to understand before the behaviour here makes sense:

  1. Twisted (Zope) Interfaces & Adapters
  2. Componentized

The counter value is stored in an Adapter class, the Interface class documents what that class provides. The reason that you can store persistent data in the Adapter is because Session (returned by getSession()) is a subclass of Componentized.

Upvotes: 3

Related Questions