tags:

views:

529

answers:

3

Hi everyone, 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!

+4  A: 

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

You can read more about componentized in http://python.net/crew/mwh/apidocs/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.

moshez
+2  A: 

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.

matburt
+1  A: 

http://www.cs.lu.se/EDA046/assignments/assignment4/twisted/picturepile.html

Check the "Session" part