浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

GAE+bottle+jinja2+beaker快速开发demo - Python,GAE - language - ITeye论坛

    :GAE+bottle+jinja2+beaker快速开发demo
    精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
    作者正文

        heartsong
        等级: 初级会员
        heartsong的博客
        性别:
        文章: 55
        积分: 80
        来自: 杭州

       发表时间:2011-02-25   最后修改:2011-02-25
    < > 猎头职位: 北京: 【北京】游戏公司诚邀php开发工程师
    相关文章: 

        在Google App Engine上用zipimport引入新版的Django
        Python Django GAE开发 环境搭建篇
        混合使用django模板和jinja模板


    推荐群组: Scala圈子
    更多相关推荐
    Python GAE

        相对于Django,bottle可以看成是一个非常精巧的python web framework了,只有一个文件就可以使用了。于是想用这个东西在gae做个简单的demo。

    1. GAE+bottle

    http://pypi.python.org/pypi/bottle

        copy那个bottle.py到gae工程的目录里,现在,可以写一个很简短的代码来测试一下:

    main.py

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

    @route
    def index():
        return 'Hello world!'

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

        同时,要修改app.yaml文件:

    handlers:
    - url: /.*
      script: main.py

        现在,就可以直接运行GAE,查看结果了!就这么简单!

    

    2. GAE+bottle+beaker

        在GAE+bottle的组合中,如果要使用session的话,查询到bottle的原话如下:
    How to implement sessions?

    There is no build in support for sessions because there is no right way to do it. Depending on requirements and environment you could use beaker middleware with a fitting backend or implement it yourself.

         很清楚的告诉我们,如果要使用session的话,可以考虑beaker,从这里下载:

    http://pypi.python.org/pypi/Beaker

         下载下来后,把里面的一个beaker文件夹,copy到GAE工程目录中,会作为一个package来使用。

        在上面的程序修改如下:

    main.py

    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()

        注意,session_opts里的session.type,如果在GAE下使用,一定要选ext:google,这个是我测试了几个选项之后才发现的。如果你有更好的方法,也欢迎告诉我,谢谢。

    

posted on 2013-12-08 01:47  lexus  阅读(393)  评论(0编辑  收藏  举报