这几天利用gae部署webpy的小东西
感慨颇多
1 gae是2.5的python
2 gae自己的框架特别类似web.py,又内置了django,但是web.py需要自己部署
3 gae对web.py并不是那么友好
部署的东西,可以参考
http://www.longtask.com/blog/?tag=gae
web.py自带的session不能用,有3种思路,
一种是刚才url里提到的memcache
但是他的sesion写法有错误,我跟了半天才看到,第33行
from web.session import Store from google.appengine.api import memcache import web import time class MemcacheStore(Store): def __init__(self, memcache): self.memcache = memcache def __contains__(self, key): data = self.memcache.get(key) return bool(data) def __getitem__(self, key): now = time.time() value = self.memcache.get(key) if not value: raise KeyError else: value['attime'] = now self.memcache.replace(key,value) return value def __setitem__(self, key, value): now = time.time() value['attime'] = now s = self.memcache.get(key) if s: #value = dict(map(lambda x: (str(x[0]), x[1]), [(k, v) for (k, v) in value.iteritems() if k not in ['_id']])) self.memcache.replace(key,value) else: #self.memcache.add(key,value,int(web.config.session_parameters['timeout'][0])) self.memcache.add(key,value,web.config.session_parameters['timeout']) def __delitem__(self, key): self.memcache.delete(key) def cleanup(self, timeout): #timeout = timeout / (24.0 * 60 * 60) #last_allowed_time = time.time() - timeout #self.collection.remove({'attime' : { '$lt' : last_allowed_time}}) #automatic cleanup the session #self.memcache.flush_all() pass
用的时候,在code.py里
from google.appengine.api import memcache
.....
store = web.session.MemcacheStore(memcache)
session = web.session.Session(app, store, initializer={'count': 0})
当然,在查的时候,陆续还发现了好几个版本,
譬如 http://wangfsec.iteye.com/blog/955673
import memcache as memcache from web.session import Store class MemcacheStore(Store): def __init__(self, config): ''' config = { 'servers': ['127.0.0.1:11211'], 'timeout': 1440 } ''' self.mc = memcache.Client(config['servers']) self.timeout = config['timeout'] def __contains__(self, key): return True if self.mc.get(key) else False def __getitem__(self, key): return self.mc.get(key) def __setitem__(self, key, value): self.mc.set(key, value, self.timeout) def __delitem__(self, key): self.mc.delete(key) def cleanup(self, timeout): '''You need nothing to do. Memcache can handle it.''' pass
还有一个版本,(忘记出处了,实在对不起这位哥)
class MemcacheStore(Store): def __init__(self): pass def __contains__(self, key): return True if memcache.get(key) else False def __getitem__(self, key): atime, v = memcache.get(key) self[key] = v return v def __setitem__(self, key, value): memcache.set(key, (time.time(), value)) def __delitem__(self, key): memcache.delete(key) def cleanup(self, timeout): pass # TODO
使用的时候,先把上面3个版本的代码选一个贴在web.session.py文件里,别忘了import memcache,
然后根据各自的init方法,在code.py里初始化就可以
譬如上面的这个初始化应该是
store = web.session.MemcacheStore()
第二种思路是利用dbstore http://code.google.com/p/gae-webpy-blog/source/browse/trunk/gae-webpy-blog/src/web/?r=3
可以看这个项目的解决方法
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))
session = web.session.Session(app, web.session.GoogleStore('colablog-session'), initializer={'user': 'anonymous', 'login': False, }, cache_handler=del_all)
第三种思路 是利用beaker这种通用session,可以参看http://simple-is-better.com/news/170
其实,最简单的嘛,还是像上面这个url里提到的一样,用google自己的session类,可惜移植性就差了
webpy官方怎么没有个正式方法呢???
补全 gae-sessions
https://github.com/dound/gae-sessions
竟然还有这种东西存在
曾经以为是gae官方的呢,后来才发现是中间件