CherryPy——基于Python的Web框架

CherryPy——基于Python的Web框架

CherryPy是什么(What is CherryPy)

CherryPy是一个基于Python的面向对象的HTTP框架。使用CherryPy来开发Web Application是非常轻松的。但CherryPy并没用提供一种类似于PHP的templating语言机制。

以下的内容摘自CherryPy Tutorial:

CherryPy is a pythonic, object-oriented HTTP framework. It provides the foundation over which complex web-based applications can be written, with little or no knowledge of the underlying protocols.

As an HTTP framework, CherryPy does all that is necessary to allow Python code to be executed when some resource (or URL) is requested by the user. However, it is not a templating language, such as PHP.

 

CherryPy的运行机制(How does CherryPy work)

CherryPy将URL映射到Python可调用对象(Python callable)来完成HTTP请求。以下的内容摘自CherryPy Tutorial:

Publishing objects
Any object that is attached to the root object is said to be published. This means that the object is accessible via the internal URL-to-object mapping routine. However, it does not mean that the object itself is directly accessible via the Web. For this to happen, the object has to be exposed.
 
Exposing objects
CherryPy maps URL requests to objects and calls the suitable method automatically. The methods that can be called as a result of external requests are said to be exposed.
 
Finding the correct object
For the user, a web application is just like a website with static files. The user types (or clicks) a URL, and gets to the desired webpage. A conventional webserver uses the URL to retrieve a static file from the filesystem. On the other hand, a web application server not only serves the content from static files; it can also map the URL it receives into some object and call it. The result is then sent back to the user's browser, where it is rendered into a viewable page. The result is a dynamic web application; for each URL, a unique object can be called into action.
The key to understand how to write a new web application is to understand how this mapping occurs. CherryPy uses a fairly straightforward mapping procedure. The root of the site is the Application.root object. When it receives a URL, it breaks it into its path components, and proceeds looking down into the site until it finds an object that is the 'best match' for that particular URL. For each path component it tries to find an object with the same name, starting from root, and going down for each component it finds, until it can't find a match.
 
The index method
The index() method has a special role in CherryPy. Like the index.html file, it's the default page for any internal node in the object tree. The index() method can take additional keyword arguments, which are automatically mapped to the form variables as sent via its GET or POST methods.
 
Calling other methods
CherryPy can also directly call methods in the published objects, if it receives a URL that is directly mapped to them.
 
Receiving data from HTML forms
Any method that is called by CherryPy - index, or any other suitable method - can receive additional data from HTML forms using keyword arguments.
CherryPy supports both the GET and POST method for forms.
 
Partial matches and the default method
Partial matches can happen when a URL contains components that do not map to the object tree.
When a partial match happens, CherryPy calls a default method.
 
The CherryPy configuration file
CherryPy uses a simple configuration file? to customize some aspects of its behavior. The configuration file can be edited with any conventional text editor (even Notepad will do it), and can be used even by non-technical users for some simple customization tasks.
 
The cherrypy structure
Most of the features of CherryPy are available through the cherrypy module. It contains several members:
·    cherrypy.engine contains the API to control the CherryPy engine.
·    cherrypy.server contains the API to control the HTTP server.
·    cherrypy.request contains the all the information that comes with the HTTP request, after it is parsed and analyzed by CherryPy.
·    cherrypy.request.headers contains a mapping with the header options that were sent as part of the request.
·    cherrypy.session is a special mapping that is automatically generated and encoded by CherryPy; it can be used to store session-data in a persistent cookie. For it to work you have to enable the session functionality by setting 'tools.session.on' to True in your config.
·    cherrypy.response contains the data that is used to build the HTTP response.
·    cherrypy.response.headers contains a mapping with the header options that will be returned by the server, before the contents get sent.

cherrypy.response.body contains the actual contents of the webpage that will be sent as a response.

Tools
CherryPy core is extremely light and clean. It contains only the necessary features to support the HTTP protocol and to call the correct object for each request. Additional features can be added to it using modular tools.
A tool is an object that has a chance to work on a request as it goes through the usual CherryPy processing chain. Several tools are provided as part of the standard CherryPy library, available in cherrypy.tools.
Tools provide a lot of flexibility. Different tools can be applied to different parts of the site, and the order of tools can be changed. The user can write custom tools for special applications, changing the behavior of CherryPy without the need to change its internals.

The tools for any part of the site are usually enabled in the configuration file.

 

CherryPy的URI分派机制(URI Dispatching)

默认CherryPy将URI映射到Python可调用对象(Python callable)。CherryPy还提供其他分派机制(参见《CherryPy Essentials:Rapid Python Web Application Development》):

  • One is set to allow applications to be developed per HTTP methods.
    (GET, POST, PUT, etc.)
  • The second is based on a popular third-party package named Routes and developed by Ben Bangert from the original Ruby implementation for Ruby on Rails.
  • The third dispatcher is a Virtual Host one, which allows dispatching based on the domain requested rather than the URI path.

CherryPy支持内嵌templating语言,参见此。

这里有一个用CherryPy和Cheetah来构建一个地址薄简单应用网站的例子。

CherryPy与Web Service

CherryPy与Ajax

A web service is an API provided by a web application so that heterogeneous user agents can interact with the application through formats other than HTML. There are different ways to create web services via REST, SOAP, XML-RPC, Atom, etc.

用CherryPy来构建Web Service并使用Ajax等技术在《CherryPy Essentials:Rapid Python Web Application Development》里有理论的以及实际的讲解,可以好好看看这本书。

后记:只是初步的了解了一下CherryPy的相关内容,理解还比较肤浅。用CherryPy来做Web开发,总的感觉是:非常自由,你有很多的选择来完成各种任务。但缺点也很明显:太自由了,很多东西得从头做起,即使有很多现成的库。不像Ruby on Rails,约定俗成,只要按照规则填东西就ok了。

更多的资料:

适用于 CGI 程序员的 CherryPy

Google搜索

你可能感兴趣的:(Python)