Example: Develop Web application on Baidu App Engine using CherryPy

时间:2023-12-24 16:59:37

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.