Session management in App Engine (or cookies)

6,411 views
Skip to first unread message

Hans-Peter

unread,
May 22, 2008, 4:14:14 AM5/22/08
to Google App Engine
Does anyone if it's possible to store session variables in app engine?
And if so how?
Cookies would also be fine.

Since my app does not require users to log in using a Google Account
there is no way to recognize a user during a session. Or is there?

Sebastian Stein

unread,
May 22, 2008, 4:21:40 AM5/22/08
to google-a...@googlegroups.com
Hans-Peter <hpha...@gmail.com> [080522 10:16]:

I don't think this is really related to GAE. Take a look at the web
framework you are using. It will definetly provide some kind of session
handling support.

Sebastian
--
http://sebstein.hpfsc.de/

Hans-Peter

unread,
May 22, 2008, 7:33:45 AM5/22/08
to Google App Engine
Mmm, this could be the problem. I don't think I am using any special
framework at all.
I just work with app engine the general way (as it is explained in the
tutorial) like this:

def main():
application = webapp.WSGIApplication( [
('/', Home),
('/links', HtmlPage),
('/admin', Admin)
], debug=True )
wsgiref.handlers.CGIHandler().run(application)

Is there any framework I should include? Or is this 'default'
framework enough to store some session variables anywhere?

Thanks,

Hans-Peter

On May 22, 10:21 am, Sebastian Stein <adv....@hpfsc.de> wrote:
> Hans-Peter <hpharm...@gmail.com> [080522 10:16]:

Ben the Indefatigable

unread,
May 22, 2008, 8:13:49 AM5/22/08
to Google App Engine
search this group on keyword "session"

bowman...@gmail.com

unread,
May 22, 2008, 8:30:50 AM5/22/08
to Google App Engine
http://gaeutilities.appspot.com/ is the demo for the sessions library
we've released to the general public. There's a link to the project
page where you can download the latest version.

luismgz

unread,
May 22, 2008, 8:33:35 AM5/22/08
to Google App Engine
This has been asked many times, and it is still a mistery...
There's a solution written by someone on this group, but as far as I
know, Google's stand on this issue is unknown.

Hans-Peter

unread,
May 23, 2008, 8:09:32 AM5/23/08
to Google App Engine
Thank you for your answers Ben and Joseph,

The Session class works just fine.
For everyone else with the same question: http://code.google.com/p/appengine-utitlies/

HP

bowman...@gmail.com

unread,
May 23, 2008, 8:46:51 AM5/23/08
to Google App Engine
Glad to see it's working out for you. I'm currently working on the .3
release of the project which will add a couple new utilities, and then
I am mulling over some additions to the session library for the .4
release.

luismgz

unread,
May 23, 2008, 3:39:18 PM5/23/08
to Google App Engine
Joseph,

Sorry for this very basic question...
I tried your demo and I extended it in order to count the hits in all
my site' pages.
But since you defined your session object within the "get" method of
"MainPage", it doesn't work in other pages, so I created it as a
global instead (outside of any class).
This works, but I doubt this is a good practice...

Another way I made it work is by creating a session object in every
page and incrementing it in 1.

So what is the right way of doing this?

luis

On May 23, 5:46 am, "bowman.jos...@gmail.com"
> > it doesn't work in othr> > search this group on keyword "session"

Jorge Vargas

unread,
May 23, 2008, 6:47:43 PM5/23/08
to google-a...@googlegroups.com
You may also want to take a look at Beaker
http://wiki.pylonshq.com/display/beaker/Home it's trunk has a GAE
store and not only it's WSGI but it's well tested (no offense to the
other modules posted) and proved to work, with pylons and other
webframeworks.

bowman...@gmail.com

unread,
May 23, 2008, 10:19:38 PM5/23/08
to Google App Engine
Luis, anywhere you want to use session, just instantiate it (session =
session.Session())

Session (plus the other utilities I'm working on) are being built with
the idea in mind that they should be able to support whatever
framework you use them in. The webapp framework used in the demo was
built directly from the examples Google provided. Just keeping it nice
and simple.

When you initialize session, it checks the browser to see if the
session id cookie exists.If it does exist, it validates it, verifying
that the same session id exists in the database (also checks the user
agent and ip). It sounds like you're probably doing the right thing...

self.session = session.Session()
session["counter"] = session["counter"] + 1

Wow, I'm so new to python, maybe someone else will come and say
session["counter"]++
exists... I don't know.


On May 23, 6:47 pm, "Jorge Vargas" <jorge.var...@gmail.com> wrote:
> You may also want to take a look at Beakerhttp://wiki.pylonshq.com/display/beaker/Homeit's trunk has a GAE

bowman...@gmail.com

unread,
May 23, 2008, 10:22:21 PM5/23/08
to Google App Engine
Jorge (and anyone else who wants to suggest an alternative to the
session class I created)

No offense taken at all! Session was really a chance for me to get my
feet wet in Python, and it was something I saw I needed. I released it
because a lot of others were asking for the same functionality. My
session class is still immature, and even I have ideas on how to
improve it. So if you know something better, please feel free to
suggest it. It gives me an opportuntity to learn more about what's
available for Python development.

On May 23, 6:47 pm, "Jorge Vargas" <jorge.var...@gmail.com> wrote:
> You may also want to take a look at Beakerhttp://wiki.pylonshq.com/display/beaker/Homeit's trunk has a GAE

luismgz

unread,
May 24, 2008, 10:04:05 AM5/24/08
to Google App Engine
Joseph,

Thanks for your reply.
My question aimed to know if the could be any problem with caching
when declaring global sessions...
I don't know yet how to think about coding for GAE...

As for the test above, I did it with weapp and it worked seamlessly.
But when I replicated it with webpy (my favorite framework) which is
very similar to webapp, I got this error:

INFO 2008-05-24 10:54:36,405 dev_appserver.py] "GET / HTTP/1.1"
500 -
Traceback (most recent call last):
File "C:\Archivos de programa\Google\google_appengine\app1\web
\application.py"
, line 186, in wsgi
result = self.handle_with_processors()
File "C:\Archivos de programa\Google\google_appengine\app1\web
\application.py"
, line 158, in handle_with_processors
return process(self.processors)
File "C:\Archivos de programa\Google\google_appengine\app1\web
\application.py"
, line 155, in process
return self.handle()
File "C:\Archivos de programa\Google\google_appengine\app1\web
\application.py"
, line 147, in handle
return self._delegate(fn, self.fvars, args)
File "C:\Archivos de programa\Google\google_appengine\app1\web
\application.py"
, line 307, in _delegate
return handle_class(f)
File "C:\Archivos de programa\Google\google_appengine\app1\web
\application.py"
, line 298, in handle_class
return tocall(*args)
File "C:\Archivos de programa\Google\google_appengine
\luis1\main.py", line 15,
in GET
self.s = session.Session()
File "/var/www/svn/pkg/appengine-utitlies-0.2/utilities/session.py",
line 60,
in __init__
if self.validateSid() != True:
File "/var/www/svn/pkg/appengine-utitlies-0.2/utilities/session.py",
line 111,
in validateSid
self.__deleteSession(sid)
NameError: global name 'sid' is not defined

What could be wrong here?

Luis

On May 23, 11:19 pm, "bowman.jos...@gmail.com"
<bowman.jos...@gmail.com> wrote:
> Luis, anywhere you want to use session, just instantiate it (session =
> session.Session())
>
> Session (plus the other utilities I'm working on) are being built with
> the idea in mind that they should be able to support whatever
> framework you use them in. The webapp framework used in the demo was
> built directly from the examples Google provided. Just keeping it nice
> and simple.
>
> When you initialize session, it checks the browser to see if the
> session id cookie exists.If it does exist, it validates it, verifying
> that the same session id exists in the database (also checks the user
> agent and ip). It sounds like you're probably doing the right thing...
>
> self.session = session.Session()
> session["counter"] = session["counter"] + 1
>
> Wow, I'm so new to python, maybe someone else will come and say
> session["counter"]++
> exists... I don't know.
>
> On May 23, 6:47 pm, "Jorge Vargas" <jorge.var...@gmail.com> wrote:
>
> > You may also want to take a look at Beakerhttp://wiki.pylonshq.com/display/beaker/Homeit'strunk has a GAE

bowman...@gmail.com

unread,
May 24, 2008, 11:50:39 AM5/24/08
to Google App Engine
Luis, I'm about to go to a wedding, so won't be able to release a bug
fix, but I think I see the problem. If you're using version .2, please
change line 111 from

self.__deleteSession(sid)
to
self.__deleteSession(self.sid)

And try it again.
> > > You may also want to take a look at Beakerhttp://wiki.pylonshq.com/display/beaker/Homeit'strunkhas a GAE
> > > store and not only it's WSGI but it's well tested (no offense to the
> > > other modules posted) and proved to work, with pylons and other
> > > webframeworks.
>
> > > On Fri, May 23, 2008 at 8:09 AM, Hans-Peter <hpharm...@gmail.com> wrote:
>
> > > > Thank you for your answers Ben and Joseph,
>
> > > > The Session class works just fine.
> > > > For everyone else with the same question:http://code.google.com/p/appengine-utitlies/
>
> > > > HP
>
> > > > On May 22, 2:33 pm, luismgz <luis...@gmail.com> wrote:
> > > >> This has been asked many times, and it is still a mistery...
> > > >> There's a solution written by someone on this group, but as far as I
> > > >> know, Google's stand on this issue is unknown.
>
> > > >> On May 22, 5:13 am, Ben the Indefatigable <bcbry...@gmail.com> wrote:
>
> > > >> > search this group on keyword "session"- Hide quoted text -
>
> - Show quoted text -

luismgz

unread,
May 24, 2008, 1:34:38 PM5/24/08
to Google App Engine
Joseph,

I hope you enjoyed the wedding!
After your recover from the hangover, please check this:

I changed line 111 as per your suggestion and then I tested it (both
with webapp and webpy).
It works, but only if I define the session outside of the class. Not
if I define it within the class.
For example, this doesn't work:

class MainPage(webapp.RequestHandler):
def get(self):
self.s = session.Session()
if not 'counter' in self.s: self.s['counter'] = 1
else: self.s['counter'] = int(self.s['counter']) + 1
self.response.out.write(self.s['counter'])

But this works:

s = session.Session()

class MainPage(webapp.RequestHandler):
def get(self):
if not 'counter' in s: s['counter'] = 1
else: s['counter'] = int(s['counter']) + 1
self.response.out.write(s['counter'])

In the first case, it doesn't show any error message.
It simply doesn't update the counter, printing always 1.

Luis




On May 24, 12:50 pm, "bowman.jos...@gmail.com"
> > > > You may also want to take a look at Beakerhttp://wiki.pylonshq.com/display/beaker/Homeit'strunkhasa GAE

luismgz

unread,
May 24, 2008, 3:31:02 PM5/24/08
to Google App Engine
And as I imagined, creating a session object in the global scope
creates problems.
I tried accessing my app from two different computers, and while the
first one was counting 10, the second one showed 11 at the first
hit...

Luis

luismgz

unread,
May 24, 2008, 3:42:56 PM5/24/08
to Google App Engine
CORRECTION:

I ignore why, but the first case now works on the published site (see
code below).
It didn't work on the development server though...
Sorry for causing confusion!

class index(app.page):
path = '/'
def GET(self):
self.s = session.Session()
if not 'count' in self.s:
self.s['count']=1
else:
self.s['count'] = int(self.s['count']) + 1

return self.s['count']

Luis


On May 24, 2:34 pm, luismgz <luis...@gmail.com> wrote:

bowman...@gmail.com

unread,
May 24, 2008, 9:51:01 PM5/24/08
to Google App Engine
No hang over, I got to drive. :) I'll see if I can get some time to
work on this, tomorrow.
> > > > - Show quoted text -- Hide quoted text -

SeanOC

unread,
May 25, 2008, 12:29:34 AM5/25/08
to Google App Engine
If your looking at using Django with App Engine you may want to check
out django-ae-utils for getting the session stuff working. You can
find it at http://code.google.com/p/django-ae-utils/. At the moment I
am the only developer and I am simply putting out tools that I create
as I find a need for them.

Right now django-ae-utils provides a sesssion backend which uses the
datastore instead of a database or memcache as well as a model and
views for an internal authentication system.

You may also want to check out Google App Engine Django (http://
code.google.com/p/google-app-engine-django/). This is a much more
ambitious project being done by a bunch of Google guys to make Django
more compatible with App Engine.

On May 24, 9:51 pm, "bowman.jos...@gmail.com"

Pankaj Vishwani

unread,
Jun 10, 2008, 6:22:00 AM6/10/08
to Google App Engine
Hi,

I am trying to use session.py in my code and it doesnt work for me.
After going through the code i found out the following:
- session if first created and then "self.ds.lastActivity" is set to
current time. (Unfortunately, I dont know how to look at the datastore
to see if the value stored is correct)
- then another request comes in and tries to validate if the session
is valid and at line 126, ie,
if self.ds.ua != os.environ['HTTP_USER_AGENT'] or self.ds.ip !
= os.environ['REMOTE_ADDR'] or sessionAge.seconds >
SESSION_EXPIRE_TIME:

sessionAge.seconds turn out to be greater than SESSION_EXPIRE_TIME
although it should have been at max 5 seconds where as
SESSION_EXPIRE_TIME= 7200.

So for some reason "self.ds.lastActivity" has wrong value.

Can someone tell me what I might be doing wrong???

Thanks,
Pankaj Vishwani


On May 24, 6:51 pm, "bowman.jos...@gmail.com"
> > > It works, but only if I define thesessionoutside of the class. Not
> > > > > >Session(plus the other utilities I'm working on) are being built with
> > > > > > the idea in mind that they should be able to support whatever
> > > > > > framework you use them in. The webapp framework used in the demo was
> > > > > > built directly from the examples Google provided. Just keeping it nice
> > > > > > and simple.
>
> > > > > > When you initializesession, it checks the browser to see if the
> > > > > >sessionid cookie exists.If it does exist, it validates it, verifying
> > > > > > that the samesessionid exists in the database (also checks the user
> > > > > > agent and ip). It sounds like you're probably doing the right thing...
>
> > > > > > self.session=session.Session()
> > > > > >session["counter"] =session["counter"] + 1
>
> > > > > > Wow, I'm so new to python, maybe someone else will come and say
> > > > > >session["counter"]++
> > > > > > exists... I don't know.
>
> > > > > > On May 23, 6:47 pm, "Jorge Vargas" <jorge.var...@gmail.com> wrote:
>
> > > > > > > You may also want to take a look at Beakerhttp://wiki.pylonshq.com/display/beaker/Homeit'strunkhasaGAE
> > > > > > > store and not only it's WSGI but it's well tested (no offense to the
> > > > > > > other modules posted) and proved to work, with pylons and other
> > > > > > > webframeworks.
>
> > > > > > > On Fri, May 23, 2008 at 8:09 AM, Hans-Peter <hpharm...@gmail.com> wrote:
>
> > > > > > > > Thank you for your answers Ben and Joseph,
>
> > > > > > > > TheSessionclass works just fine.

Pankaj Vishwani

unread,
Jun 10, 2008, 6:32:38 AM6/10/08
to Google App Engine
Continued from the post above..............
I dont know if this will help but sessionAge.seconds always comes out
to be 50448.

Thanks,
Pankaj Vishwani


On Jun 10, 3:22 am, Pankaj Vishwani <pankajvishw...@gmail.com> wrote:
> Hi,
>
> I am trying to usesession.py in my code and it doesnt work for me.
> After going through the code i found out the following:
> -sessionif first created and then "self.ds.lastActivity" is set to

bowman...@gmail.com

unread,
Jun 10, 2008, 3:57:05 PM6/10/08
to Google App Engine
I need some more information.

What version of appengine-utilities are you using?
Is this happening on your local development SDK and when uploaded to
appengine?
Can you give me a code sample of how you're using session.py?

It would be easier for me to track this if you filed an issue at
http://code.google.com/p/appengine-utitlies/issues/list

Offhand, I haven't seen a problem like this before.

Pankaj Vishwani

unread,
Jun 10, 2008, 4:59:34 PM6/10/08
to Google App Engine
Here are the details you asked for:

appengine-utilities: 0.3.1
It is happening to my local developement SDK. I haven't uploaded it on
the appengine yet.

Usage:
I have put your session.py file in my applicationManager pacakage.
And then there's a Main.py that is called for all the urls. there I
have imported the session

from applicationManager import session

class X(webapp.RequestHandler):
def get(self):
self.sess = session.Session()
self.sess['myVar'] = "test"

Now when the request comes here for the first time, it does create a
new session and saves the value 'test'. And in the second request that
comes in, your session class loads the right session with the value
'test' in it but the lastActivity that it fetches is different than
what was saved therefore it is not able to validate the session and
deletes the session.

Let me know if you need more info...


Regards,
Pankaj Vishwani










On Jun 10, 12:57 pm, "bowman.jos...@gmail.com"
<bowman.jos...@gmail.com> wrote:
> I need some more information.
>
> What version of appengine-utilities are you using?
> Is this happening on your local development SDK and when uploaded to
> appengine?
> Can you give me a code sample of how you're usingsession.py?
>
> It would be easier for me to track this if you filed an issue athttp://code.google.com/p/appengine-utitlies/issues/list

bowman...@gmail.com

unread,
Jun 10, 2008, 8:21:57 PM6/10/08
to Google App Engine
My replies will go on the issue you filed, as it really is easier for
me to track it there. Thank you.
> ...
>
> read more »

Pankaj Vishwani

unread,
Jun 15, 2008, 9:32:41 PM6/15/08
to Google App Engine
I have encountered one more issue.
For some reason, the session = session.Session(), returns me the
result really slow (I figured it out using eclipse debug mode).
Also, any update, delete of key, value pair is slow. Is there
something I should do???

One thing I noticed is that you are not using name="key" for the
Models that you are using.

Any suggestions to increase the request-response is highly
appreciated.

Environment that I am working on and testing stuff is the following:
OS: Window-vista
appengine-utitlies: 0.3.1
google SDK: 1.1.0

I haven't yet uploaded my code on the app-enigne as of now so have no
idea how it respond there.



Regards,
Pankaj Vishwani



On Jun 10, 5:21 pm, "bowman.jos...@gmail.com"
<bowman.jos...@gmail.com> wrote:
> My replies will go on the issue you filed, as it really is easier for
> me to track it there. Thank you.
>
> On Jun 10, 4:59 pm, Pankaj Vishwani <pankajvishw...@gmail.com> wrote:
>
> > Here are the details you asked for:
>
> > appengine-utilities: 0.3.1
> > It is happening to my local developement SDK. I haven't uploaded it on
> > the appengine yet.
>
> > Usage:
> > I have put yoursession.py file in my applicationManager pacakage.
> > And then there's a Main.py that is called for all the urls. there I
> > have imported thesession
>
> > from applicationManager importsession
>
> > class X(webapp.RequestHandler):
> > def get(self):
> > self.sess =session.Session()
> > self.sess['myVar'] = "test"
>
> > Now when the request comes here for the first time, it does create a
> > newsessionand saves the value 'test'. And in the second request that
> > comes in, yoursessionclass loads the rightsessionwith the value
> ...
>
> read more »

nchauvat (Logilab)

unread,
Jun 16, 2008, 4:27:24 AM6/16/08
to Google App Engine
> I haven't yet uploaded my code on the app-enigne as of now so have no
> idea how it respond there.

Since your code will be running on Google servers, I am not sure I see
the point in trying to optimize it for the SDK that's just there to
provide the APIs. Unless your are doing something that's obviously
very time-consuming and could be shortened when processing your data,
just upload your code and test its performance on the real thing
instead.

bowman...@gmail.com

unread,
Jun 16, 2008, 3:17:10 PM6/16/08
to Google App Engine
I think this might be another vista issue. Have you tried the session
demo to see if you see any slowness with that? My experience has
always been it's been pretty instantaneous.
> ...
>
> read more »

Rob Conner

unread,
Jun 29, 2008, 12:59:20 AM6/29/08
to Google App Engine
I can't believe some of the things google chose not to include in the
app engine extentions.

Pankaj, yeah don't complain until you upload it and try it. The dev
server is not meant to be fast, only easy to use.
Reply all
Reply to author
Forward
0 new messages