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

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 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/p/3463525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值