Example: Develop Web application on Baidu App Engine using CherryPy

In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Google App Engine, or Nitrous.Io, the documentation of BAE is really not good enough. The only advantage of BAE is stable - you needn't to worry about GFW - on Mainland China. I'm used to CherryPy to do simple web applications, and after some attempts, I figure out how to deploy it to BAE:

1. Create a deployment on BAE

Remember to select the type as python-web. 

2. Checkout and modify code

When done, notice these files:

requirements.txt

As the documentation says, this is where we declare the library dependencies we want. So we just write:

cherrypy

app.conf

Here to let it work, modify it like this:

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

  - expire : .jpg modify 10 years
  - expire : .swf modify 10 years
  - expire : .png modify 10 years
  - expire : .gif modify 10 years
  - expire : .JPG modify 10 years
  - expire : .ico modify 10 years

The key here is that url property should be /* rather than /. And notice index.py is the entrance of your application. 

index.py

Basiclly, this file will look like this:

#-*- coding:utf-8 -*-
import cherrypy
import script

try:
    from bae.core.wsgi import WSGIApplication
    app = cherrypy.tree.mount(script.HelloWorld(environment), "/", config=script.CONFIG)
    application = WSGIApplication(app)
except ImportError:
    cherrypy.quickstart(script.HelloWorld(environment), config=script.CONFIG)

Here the 'script.py' is just a file I created to handle the actual logic. The key here is WSGIApplication which is offered by BAE, and we should wrap our app created by CherryPy framework with it. Using try... except structure here, we can use same code both on BAE and on local environment, which makes debugging more convenient. 

3. Problem remain: Session

It seems that the session function will not work very well on BAE, I've tried different configurations but can't make it. Next time I will go into it and try to figure out the reason.

转载于:https://www.cnblogs.com/toowhite/p/3861484.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值