Cherrypy 是嘛东西?

CherryPy 是个典型的面向对象的 HTTP 开发框架.

CherryPy 可以让开发者可以象开发类似其他Python面向对象的程序那样简单并快速地去开发一个Web应用. 下面就是一个简单的例子:

  1. import cherrypy
  2. class HelloWorld:
  3. def index(self):
  4. return "Hello World!"
  5. index.exposed = True
  6. cherrypy.quickstart(HelloWorld())

Start the application at the command prompt(after navigating to its folder):

python hello.py

然后 使用浏览器访问地址: http://localhost:8080

The rendering:

Hello World!

这时, 在命令行窗口上按下 Ctrl+C 键便可以终止程序的运行.

代码 import cherrypy 将引入 CherryPy 模块扩展.

HelloWorld 类的定义则会将完成类的方法的发布.

有一个必须定义的 index() 方法则默认为URL(如: http://localhost:8080)中的网站root, 该方法返回的字符串将做为网页中的 内容 (如本例中的 ‘Hello World!’).

代码 index.exposed = True 则表明 index() 方法是要开放出去的.

  • 只有开放出去的方法,才可以处理各种HTTP请求.

  • 让开发人员自主控制对象的方法是否开放为Web请求更简单快捷.

  • 在方法前使用代码 @cherrypy.expose 同样也能立即发布该方法:

  1. @cherrypy.expose
  2. def index(self):
  3. return "Hello World!"

代码 cherrypy.quickstart(HelloWorld()) 是将对象做为 CherryPy 的HTTP Handle 启动 CherryPy 的 Web 服务.

发布 HelloWorld 类的实例

  • 启动内嵌 webserver

  • 直到按(Ctrol+C)键结束运行

当应用运行时, CherryPy 是以默认配置运行的.

  • Listening on localhost at port 8080

  • 这些默认值都可以使用配置文件进行覆盖修改

    • cherrypy.config.update({‘server.socket_port’:8010})

    • 这样就将监听端口修改为8010了.

服务器的访问地址为: http://localhost:8080

  • HelloWorld 类中找最匹配的方法来处理Web请求.

  • 调用 HelloWorld().index() 方法.

  • 方法返回给请求用户的浏览器内显示.

Cherrypy 应用实例

CherryPy 可以让你开发出内嵌Python标准应用多线程Web服务的强大的Web应用. 你可以在任何可以运行Python程序的环境下布曙它. 并不需要Apache, 但你可以在Apache(或者 lightpd 甚至 IIS)中调用 CherryPy 应用. 可以运行于 Windows, Linux, Max OS X 以及其他的支持 Python 语言的操作系统.

除此之外, CherryPy 还会原汁原味地保留你的应用. 你可以自由地使用各种各样的模板,数据存取等技术方案. CherryPy 也能处理诸如 Session, 静态文件, Cookie, 文件上传等几乎所有 Web 应用中涉及到的东西.

Features

  • A fast, HTTP/1.1-compliant, WSGI thread-pooled webserver. Typically, CherryPy itself takes only 1-2ms per page!

  • Support for any other WSGI-enabled webserver or adapter, including Apache, IIS, lighttpd, mod_python, FastCGI, SCGI, and mod_wsgi

  • Easy to run multiple HTTP servers (e.g. on multiple ports) at once

  • A powerful configuration system for developers and deployers alike

  • A flexible plugin system

  • Built-in tools for caching, encoding, sessions, authorization, static content, and many more

  • A native mod_python adapter

  • A complete test suite

  • Swappable and customizable…everything.

  • Built-in profiling, coverage, and testing support.

Consider these examples (root is conceptual, referring to the root of the document tree),

root = HelloWorld()

root.onepage = OnePage()

root.otherpage = OtherPage()

URL http://localhost:8080/onepage points at the 1st object,

URL http://localhost:8080/otherpage points at the 2nd.

Consider,

root.some = Page()

root.some.page = Page()

URL http://localhost:8080/some/page is mapped to the root.some.page object. If this object is exposed (or its index method is), it’s called for that URL

In our HelloWorld example, adding the http://…/onepage to OnePage() mapping could be done as:

  1. class OnePage():
  2. def index(self):
  3. return "one page!"
  4. index.exposed = True
  5. class HelloWorld(object):
  6. onepage = OnePage()
  7. def index(self):
  8. return "hello world"
  9. index.exposed = True
  10. cherrypy.quickstart(HelloWorld())

In the address bar of the browser, put http://localhost:8080/onepage

The Index Method

  • Method index(), like the index.html file, is the default page for any internal node in the object tree

  • Can take additional keyword arguments, mapped to the form variables as sent via its GET or POST methods

  • It’s only called for a full match on the URL

Calling Other Methods

CherryPy can also directly call methods in the published objects if it receives a URL that is directly mapped to them—e.g.,

  1. class HelloWorld():
  2. def index(self):
  3. return "Hello World!"
  4. index.exposed = True
  5. @cherrypy.expose
  6. def test(self):
  7. return "Test Controller"
  8. cherrypy.quickstart(HelloWorld())

Then request http://localhost:8080/test

When CherryPy receives a request for the /test URL, it calls the test() function.

  • It can be a plain function, or a method of any object—any callable will do.

If CherryPy finds a full match and the last object in the match is a callable.

  • A method, function, or any other Python object that supports the __call__ method and the callable doesn’t contain a valid index() method.

Then the object itself is called.

These rules are needed because classes in Python are callables (for producing instances).

CherryPy supports both the GET and POST method for forms.