[Twisted-web] Re: Application variables?

Nicola Larosa nico at tekNico.net
Fri Apr 2 02:39:10 MST 2004

> That is, where do I declare and access variables to hold information that 
> is to be shared by all requests?

Look at twisted/web/server.py , the second to last class is the Session one
(in v.1.1.1).

You can see that the constructor has a "sessionNamespaces" dictionary
attribute, nowhere used in the Twisted code, that looks like the right place
(undocumented, that I know of).

Here's how I use it:

sessionNSs = request.getSession().sessionNamespaces
appSect1Session = sessionNSs.setdefault('appSect1', {})
appSect1Session['whatever'] = 'whatever'

Now appSect1Session is another dictionary where a given app section can put
whatever objects it wants, and get to them later.

sessionNSs = request.getSession().sessionNamespaces
    whatever = sessionNSs['appSect1']['whatever']
except KeyError:
    whatever = None

or something.

(The browser will have to support cookies for this to work, obviously.)

Nicola Larosa - nico at tekNico.net

"...programs must be written for people to read,
and only incidentally for machines to execute."
 -- Alan J. Perlis, SICP

More information about the Twisted-web mailing list