Session@GAE

Google的App Engine是不允许文件写入的,想实现session只有Big Table或者是基于此的GData了,

gae-sessions https://github.com/dound/gae-sessions#readme 就是这样一个session库,

示例,

from gaesessions import get_current_session
session = get_current_session()
if session.is_active():
c = session.get('counter', 0)
session['counter'] = c + 1
session['blah'] = 325
del session.blah # remove 'blah' from the session
# model instances and other complex objects can be stored too

# If you don't care if a particular change to the session is persisted
# to the datastore, then you can use the "quick" methods. They will
# only cause the session to be stored to memcache. Of course if you mix
# regular and quick methods, then everything will be persisted to the
# datastore (and memcache) at the end of the request like usual.
session.set_quick('x', 9)
x = session.get('x')
x = session.pop_quick('x')

# ...
# when the user logs in, it is recommended that you rotate the session ID (security)
session.regenerate_id()

-------

但是作为WSGI的用得最多的session库应该是beaker,其实他也是支持GAE的

示例 Bottle + GAE,

from bottle import route, default_app
from beaker.middleware import SessionMiddleware
from google.appengine.ext.webapp.util import run_wsgi_app

@route('/')
def index():
    session = request.environ['beaker.session']
    if 'refrush_times' in session:
        refrush_times = int(session['refrush_times'])
    else:
        refrush_times = 0
    refrush_times = refrush_times + 1
    session['refrush_times'] = refrush_times
    return 'Hello world! You have refrush this page for %s times.' % str(refrush_times)

def main():
    '''Remove this when on production '''
    bottle.debug(True)
    app = default_app()

    session_opts = {
                    'session.type': 'ext:google',
                    'session.cookie_expires': True,
                    'session.auto': True,
                    }
    app = SessionMiddleware(app, session_opts)
    run_wsgi_app(app)
if __name__ == '__main__':
    main()

用标准库吧,这样你的程序好移植些

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。