python -c "import cherrypy;print cherrypy.__version__"
CherryPy是一个Python的HTTP框架,可以用Python来处理HTTP请求然后返回结果。在qbo_webi应用中用到了这个服务器。
参考 http://www.cnblogs.com/JeffreySun/archive/2009/12/24/1631431.html,然后在ubuntu下面进行了实验。
1.安装方法
ubuntu 安装
Packages to be installed:
CherryPy:
apt-get install python-cherrypy3
Mako:
apt-get install python-mako
如何查看cherrypy版本号:
dpkg -l | grep -i cherrypy
How do I configure the ip address with CherryPy?
使用conf文件的方法。cherrypy.server.socket_host = 'www.machinename.com'
cherrypy.engine.start()
或cherrypy.server.socket_host = '0.0.0.0'
2.入门点 Hello World分析
先新建一个HelloWorld.py文件。代码如下:
1: import cherrypy
2:
3: class HelloWorld:
4: @cherrypy.expose
5: def hello(self):
6: return "Hello World!"
7:
8: cherrypy.quickstart(HelloWorld())
执行结果:
ppeix:src$ cat HelloCheery.py
#!/usr/bin/env python
# coding: utf-8
# Software License Agreement (GPLv2 License)
import cherrypy;
class HelloWorld:
@cherrypy.expose
def hello(self):
return "Hello World!";
cherrypy.quickstart(HelloWorld())
ppeix:src$ ./HelloCheery.py
[24/Sep/2014:11:43:01] ENGINE Listening for SIGHUP.
[24/Sep/2014:11:43:01] ENGINE Listening for SIGTERM.
[24/Sep/2014:11:43:01] ENGINE Listening for SIGUSR1.
[24/Sep/2014:11:43:01] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[24/Sep/2014:11:43:01] ENGINE Started monitor thread 'Autoreloader'.
[24/Sep/2014:11:43:01] ENGINE Started monitor thread '_TimeoutMonitor'.
[24/Sep/2014:11:43:01] ENGINE Serving on 127.0.0.1:8080
[24/Sep/2014:11:43:01] ENGINE Bus STARTED
逐行解释下代码:
- 第1行,这个是必须的,导入CherryPy的主模块。
- 第4行,HelloWorld类中有一个hello方法,hello这个方法名会对应到请求的URL中的内容。http://localhost:8080/hello 会导致调用hello方法。这些要被调用的方法需要让它被发布出来,可以通过给一个@cherrypy.expose标记,或者直接hello.expose = True进行设置也可以,下面的代码和HelloWorld.py中的代码等同:
1: import cherrypy
2:
3: class HelloWorld:
4: def hello(self):
5: return "Hello World"
6:
7: hello.exposed = True
8:
9: cherrypy.quickstart(HelloWorld())
- 第6行,return的字符串直接返回到客户端做为请求的结果。
- 第8行,发布一个HelloWorld的实例,并启动内置的Web服务器。
demo1
1: import cherrypy
2:
3: class HelloWorld:
4: @cherrypy.expose
5: def hello(self):
6: return "hello"
7:
8: def index(self):
9: return "Hello world!"
10: index.exposed = True
11:
12: cherrypy.quickstart(HelloWorld())
这里有一个hello和index方法,hello方法对应的URL是http://localhost:8080/hello。index方法对应的URL是http://localhost:8080/index,但是index这个方法也对应到http://localhost:8080这个地址,相当于一个默认的方法,类似于IIS中设置的index.html、default.html等页面。
demo2
1: import cherrypy
2:
3: class HelloWorld:
4: @cherrypy.expose
5: def hello(self,firstname,lastname):
6: return "i love you"+" : "+firstname+lastname
7:
8: @cherrypy.expose
9: def default(self, year, month, day,aa):
10: return "error";
11: cherrypy.quickstart(HelloWorld())
- hello方法,注意后面的firstname和lastname两个参数,这个两个参数对应到浏览器的参数。这个hello方法对应到http://localhost:8080/hello/Jeffery/Sun或者http://localhost:8080/hello?firstname=Jeffery&lastname=Sun。注意,当把参数直接作为地址的一部分传递时(第一个URL),CherryPy会根据 “/”分开URL赋值给方法参数。但是如果用浏览器参数形式传递(第二个URL),浏览器参数名称必须能和方法参数名对应上。
- default方法,default方法有特殊的含义,它本身除了一般的方法的意思之外,还用于匹配与它参数个数一致的请求,一般可以用来作为非法URL的错误处理。上面的default方法可以对应到这个URL:http://localhost:8080/default/2007/10/20/lijianwei,参数year对应到2007,month对应到10,day对应到20,aa对应到lijianwei。但是它也对应到如下的几个URL:http://localhost:8080/aaa/bbb/ccc/dddd和http://localhost:8080/111/222/333/444等,也就是说虽然这两个URL对应不到任何的一个Python的方法,但是由于分开后的参数个数能与default方法对应上,所以还是会匹配到default方法,这就是default方法的一个特殊作用。一个正常的请求到达后,首先进行正常的匹配,看能否找到一个合适的方法,如果没有,那么就会去和default匹配,看参数个数是否一样,如果还是不匹配就会抛出404错误。