[Twisted-Python] Re: twisted.web session expiry

David Bolen db3l.net at gmail.com
Fri Aug 22 14:08:50 MDT 2008


Daniel Browne <grimsqueaker13 at gmail.com> writes:

> How can I do something (eg. redirect) once a users session has
> expired? At the point when the session expires your request is
> finished and your session is expired. I have added the notification
> callback but I can't really do anything inside it without a session to
> store information in or a request to redirect with, so I just print a
> message to the console.

If you're trying to cause something to happen on the user's browser
when their session expires, as others have pointed out default HTTP is
stateless, so without something (like a bit of Javascript) running in
the user's browser staying in contact with your server behind the
scenes you won't be able to trigger any behavior on their end.

A user is likely to be already familiar with how most sites that have
active sessions won't tell them that their session is expired until
they initiate a subsequent request to the server (refresh a page, try
to click on a link, etc...).  Barring unique requirements for your
application, simply behaving that way - check the session for possible
expiration upon any subsequent request from that user and issue the
redirect then if expired - should be a fairly common behavior.


If you want to have something on the server end occur when the session
expires (say to update some internal state, or adjust or clear the
session information), then you can just establish a standard Twisted
callLater callback (which could itself be stored in the session and
reset when requests did come in), or if you may have lots of sessions,
have a separate server side object with a single periodic timer take
care of expiring sessions.  Or any other server side event that occurs
could be used to initiate session expiration processing.

-- David





More information about the Twisted-Python mailing list