Beaker sessions in Appengine

71 views
Skip to first unread message

lui...@gmail.com

unread,
Oct 24, 2008, 8:48:37 AM10/24/08
to web.py
I'm looking for information about how to use Beaker sessions with
webpy on Appengine.
I've been googling for awhile but I could only find incomplete or
outdated information.
I know people are using beaker sessions in appengine but so far, I
could only get it working with a patch for webapp (it doesn't work
with webpy).

If there is anyone using beaker, please kindly tell me what version
should I use and, if possible, some usage sample (configuration
options, etc).

Thanks!
Luis

Brent Pedersen

unread,
Oct 24, 2008, 1:19:57 PM10/24/08
to we...@googlegroups.com
if you dont need beaker, you can use this one i just hacked together.
i added it to the app i stole from aaronsw to mess with appengine (and
use mako).

http://webpy-mako.appspot.com/

the Store class is in the source:
http://webpy-mako.appspot.com/source

maybe someone else can comment if i've implemented the store correctly.

regarding appengine and web.py, is anyone running tests? i find i get
an error about socket._fileobject which appengine doesnt support.
anyone found a way around that? i guess i may just start the server
and use twill to browse around the site...

-brentp

Brent Pedersen

unread,
Oct 24, 2008, 1:20:24 PM10/24/08
to we...@googlegroups.com
that uses memcache by the way.

lui...@gmail.com

unread,
Oct 24, 2008, 1:50:37 PM10/24/08
to web.py
Thanks! I tested it, and it seems that after a few clicks the count
starts at 1 again.
I don't know why, but I read in other groups that memcache alone is
not reliable enough for sessions...


On Oct 24, 2:20 pm, "Brent Pedersen" <bpede...@gmail.com> wrote:
> that uses memcache by the way.
>
> On Fri, Oct 24, 2008 at 10:19 AM, Brent Pedersen <bpede...@gmail.com> wrote:
> > if you dont need beaker, you can use this one i just hacked together.
> > i added it to the app i stole from aaronsw to mess with appengine (and
> > use mako).
>
> >http://webpy-mako.appspot.com/
>
> > the Store class is in the source:
> >http://webpy-mako.appspot.com/source
>
> > maybe someone else can comment if i've implemented the store correctly.
>
> > regarding appengine and web.py, is anyone running tests? i find i get
> > an error about socket._fileobject which appengine doesnt support.
> > anyone found a way around that? i guess i may just start the server
> > and use twill to browse around the site...
>
> > -brentp
>

yejun

unread,
Oct 24, 2008, 2:44:16 PM10/24/08
to web.py
Webpy's own session work well. I don't think you need to use beaker's.
Here is the session I patched for webpy.
http://github.com/yejun/webpy/tree/GAE/web/session.py

Use it as normal webpy session.
app = web.application(urls, globals())
session = web.session.Session(app)

lui...@gmail.com

unread,
Oct 24, 2008, 3:56:57 PM10/24/08
to web.py
A BIG THANKS!

By the way, this doesn't work with webpy 0.3 right?
I got this error:

<type 'exceptions.ImportError'>: No module named flup.server.fcgi
args = ('No module named flup.server.fcgi',)
message = 'No module named flup.server.fcgi'

Is there anything else I should install besides flup?

Luis

On Oct 24, 3:44 pm, yejun <yej...@gmail.com> wrote:
> Webpy's own session work well. I don't think you need to use beaker's.
> Here is the session I patched for webpy.http://github.com/yejun/webpy/tree/GAE/web/session.py

yejun

unread,
Oct 24, 2008, 4:32:22 PM10/24/08
to web.py
You should not see that error if you use latest 0.3.

app = web.application(urls, globals())
session = web.session.Session(app)
def main():
app.cgirun()

To use latest 0.3 with template, you simply remove all line contains
compiler in template.py

lui...@gmail.com

unread,
Oct 24, 2008, 4:38:46 PM10/24/08
to web.py
I'm checking out this problem which is surely not related to your
script.
I don't know why, the sequence of function calls in errors page shows
that the main scripts attempts to import modules in another site's
folder...
> > > > Luis- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

lui...@gmail.com

unread,
Oct 24, 2008, 4:44:42 PM10/24/08
to web.py
I noted that you used "app.cgirun" in your main function, while I was
using "app.run" instead.
I edited this line, and now I got this:

AttributeError: 'module' object has no attribute 'safestr'

INFO 2008-10-24 21:39:12,971 dev_appserver.py] "GET / HTTP/1.1"
500 -
INFO 2008-10-24 21:39:13,049 dev_appserver_index.py] Updating C:
\Program Fil
es\Google\google_appengine\luis2\index.yaml

But I am not using the latest 0.3. I'm using an old one because the
latest ones didn't work with appengine (I used to get errors related
to a parser...).
Do you think that changing to the last version and removing all the
lines you indicated above will solve the problem?

Luis

lui...@gmail.com

unread,
Oct 24, 2008, 4:48:20 PM10/24/08
to web.py
This is the whole error message:


AttributeError: 'module' object has no attribute 'safestr'

INFO 2008-10-24 21:45:40,266 dev_appserver.py] "GET / HTTP/1.1"
500 -
INFO 2008-10-24 21:45:40,344 dev_appserver_index.py] Updating C:
\Program Fil
es\Google\google_appengine\luis2\index.yaml
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\luis2\web
\application.py", line
186, in wsgi
result = self.handle_with_processors()
File "C:\Program Files\Google\google_appengine\luis2\web
\application.py", line
158, in handle_with_processors
return process(self.processors)
File "C:\Program Files\Google\google_appengine\luis2\web
\application.py", line
153, in process
return p(lambda: process(processors))
File "C:\Program Files\Google\google_appengine\luis2\web
\session.py", line 58,
in _processor
self._load()
File "C:\Program Files\Google\google_appengine\luis2\web
\session.py", line 83,
in _load
self.session_id = self._generate_session_id()
File "C:\Program Files\Google\google_appengine\luis2\web
\session.py", line 124
, in _generate_session_id
session_id = sha1("%s%s%s%s" %(rand, now,
utils.safestr(web.ctx.ip), secret_
key))
AttributeError: 'module' object has no attribute 'safestr'

INFO 2008-10-24 21:46:28,848 dev_appserver.py] "GET / HTTP/1.1"
500 -
INFO 2008-10-24 21:46:28,926 dev_appserver_index.py] Updating C:

Brent Pedersen

unread,
Oct 24, 2008, 4:59:22 PM10/24/08
to we...@googlegroups.com
hmm. youre right, i didnt notice that. yejun's version looks better anyway.

yejun

unread,
Oct 24, 2008, 5:04:56 PM10/24/08
to web.py
That error you got from latest 3.0 is related to template.py.
You can just remove line 35 and 875 from template.py.

yejun

unread,
Oct 24, 2008, 5:11:30 PM10/24/08
to web.py
Actually your line number might be different. Just search compiler in
template.py and remove them.

lui...@gmail.com

unread,
Oct 24, 2008, 5:14:45 PM10/24/08
to web.py
Alright,

1) I started all over again with a clean install and the latest
version of 0.3.
2) I edited template.py by deleting lines 35 and 875.

but I keep on getting errors...

<type 'exceptions.AttributeError'>: 'module' object has no attribute
'get_magic'
args = ("'module' object has no attribute 'get_magic'",)
message = "'module' object has no attribute 'get_magic'"

yejun

unread,
Oct 24, 2008, 5:25:09 PM10/24/08
to web.py
Probably I did something else I don't remember.
Copy the one from my git, which should work.
http://github.com/yejun/webpy/tree/GAE/web/template.py

Anand Chitipothu

unread,
Oct 24, 2008, 5:46:27 PM10/24/08
to we...@googlegroups.com
I fixed templetor to work on GAE.
Now it is also possible to use templates on GAE by generating python
code for templates, which can be done by running the following
command.

python web/template.py --compile templates

It generates a python file templates/__init__.py.

On GAE web.template.render function is redefined to return the
templates module instead of the Render object.
Since the generated templates module is exactly identical to that
Render in behavior, no code changes are required to run web.py
application on GAE.

Please let me know if anything breaks.

lui...@gmail.com

unread,
Oct 24, 2008, 5:50:01 PM10/24/08
to web.py
OK, i'M ALMOST THERE!
But...

session = web.session.Session(app)
TypeError: __init__() takes at least 3 arguments (2 given)
INFO 2008-10-24 22:24:10,558 dev_appserver.py] "GET / HTTP/
1.1" 500 -

Actually, I use an initializer parameter ( "initializer={'count':
0}" ), but I'm still lacking a third parameter. What's missing?

In few words, I want to test the sample script as shown in the
cookbook. What should I modify in order to use it in Appengine (see
below):

import web
urls = (
"/count", "count",
"/reset", "reset"
)
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'),
initializer={'count': 0})

class count:
def GET(self):
session.count += 1
return str(session.count)

class reset:
def GET(self):
session.kill()
return ""

if __name__ == "__main__":
app.run()







On Oct 24, 6:25 pm, yejun <yej...@gmail.com> wrote:
> Probably I did something else I don't remember.
> Copy the one from my git, which should work.http://github.com/yejun/webpy/tree/GAE/web/template.py

yejun

unread,
Oct 24, 2008, 5:56:48 PM10/24/08
to web.py
session = web.session.Session(app, initializer={'count':0})

There is no diskstore on google. You are using the session.py from
original webpy not mine.

Anand Chitipothu

unread,
Oct 24, 2008, 5:57:02 PM10/24/08
to we...@googlegroups.com
On Sat, Oct 25, 2008 at 3:20 AM, lui...@gmail.com <lui...@gmail.com> wrote:
>
> OK, i'M ALMOST THERE!
> But...
>
> session = web.session.Session(app)
> TypeError: __init__() takes at least 3 arguments (2 given)
> INFO 2008-10-24 22:24:10,558 dev_appserver.py] "GET / HTTP/
> 1.1" 500 -

I am getting a different error.

File "/Users/anand/work/webpy/gae/helloworld/web/session.py", line
184, in __init__
os.mkdir(root)
AttributeError: 'module' object has no attribute 'mkdir'

I think it is because app engine doesn't allow you to create
directories or write files.

lui...@gmail.com

unread,
Oct 24, 2008, 6:10:04 PM10/24/08
to web.py
You are right!
It works like a charm now. Thank you very much!

yejun

unread,
Oct 24, 2008, 6:10:23 PM10/24/08
to web.py
It is only possible to use memcache or datastore from google on GAE.
Brent Pedersen implemented a memcache store class in his code.
Here is the implementation for google store. The only caveat in
original session file is the cleanup time is too long, deletion on GAE
store need to be executed on a per recorded basis.

class WebpySession(db.Model):
data = db.BlobProperty()
atime = db.DateTimeProperty(auto_now=True)

class GoogleStore(Store):
"""Google Datastore"""
def __init__(self, prefix="p"):
self.prefix=prefix

def __contains__(self, key):
return WebpySession.get_by_key_name(self.prefix+key) is not
None

def __getitem__(self, key):
try:
return
pickle.loads(WebpySession.get_by_key_name(self.prefix+key).data)
except AttributeError:
raise KeyError, key

def __setitem__(self, key, value):
r = WebpySession.get_or_insert(self.prefix+key)
r.data = pickle.dumps(value)
r.put()

def __delitem__(self, key):
try:
WebpySession.get_by_key_name(self.prefix+key).delete()
except AttributeError:
pass
def cleanup(self, timeout):
db.delete(WebpySession.all().filter('atime <',
datetime.datetime.now()-
datetime.timedelta(seconds=timeout)).fetch(CLEANUP_BATCH_SIZE))


On Oct 24, 5:57 pm, "Anand Chitipothu" <anandol...@gmail.com> wrote:

lui...@gmail.com

unread,
Oct 24, 2008, 6:19:33 PM10/24/08
to web.py
What are the advantages or disadvantages of using memcache for
sessions?
I see that other people are implementing both approaches combined,
although the ones I tested are buggy.

And by the way, Is it possible to create a GAE branch with Yejun's and
Anand's patches?

yejun

unread,
Oct 24, 2008, 6:26:09 PM10/24/08
to web.py
Memcache's data is not persistent and may lose at any time, so it
depends what's the session is used for.
A combined version should work, however the advantage isn't clear.
Unless there is a lot read access without write back. Even that I
believe datastore read is already cached by google.

paul jobs

unread,
Oct 24, 2008, 6:27:58 PM10/24/08
to we...@googlegroups.com
it ll be great if gae webpy is a separate live branch

yejun

unread,
Oct 24, 2008, 6:52:53 PM10/24/08
to web.py
Very neat idea, but the debug view still is not working.
Reply all
Reply to author
Forward
0 new messages