---
title: "pythonic cherrypy"
author: "huangyiiiiii"
date: "2007-03-09T13:20:21Z"
tags: []
slug: "pythonic-cherrypy"
draft: false
lastmod: "2007-03-09T13:20:21Z"
id: 58700
url: "https://codeplayer.iteye.com/blog/58700"
---

刚才看到这个页面：http://tools.cherrypy.org/wiki/InteractiveInterpreter，发现 cherrypy 还可以这样做，真是有点意思，正如作者所说： We think it showcases the pythonic nature of [CherryPy](http://tools.cherrypy.org/wiki/CherryPy).  
不过那个视频使用的是cherrypy以前的版本，cherrypy3稍有不同，以下是我使用 cherrypy3 在 ipython 上实验的结果：

```
Python 2.4.4 Stackless 3.1b3 060516 (#71, Jan 27 2007, 21:48:58) [MSC v.1310 32
bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 0.7.3 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import cherrypy

In [2]: cherrypy.config.update({
   ...: 'autoreload.on':False,
   ...: 'server.log_to_screen':False
   ...: })

In [3]: class Hello(object):
   ...:     @cherrypy.expose
   ...:     def index(self):
   ...:         return 'hello world!'
   ...:     @cherrypy.expose
   ...:     def test(self):
   ...:         yield 'test1'
   ...:         yield 'test2'
   ...:

In [4]: hello = Hello()

In [5]: cherrypy.tree.mount(hello, '/')
Out[5]: <cherrypy._cptree.Application object at 0x00E2C0F0>

In [6]: cherrypy.engine.start(blocking=False)
CherryPy Checker:
The Application mounted at '' has an empty config.


In [7]: cherrypy.server.quickstart()
[09/Mar/2007:21:01:40] HTTP Serving HTTP on http://0.0.0.0:8080/

# 注释：此时可以访问 http://localhost:8080/ 和 http://localhost:8080/test 了。

In [8]: def test2(self):
   ...:     return 'test2'
   ...:

In [9]: Hello.test2 = cherrypy.expose(test2)
# 注释：此时可以访问 http://localhost:8080/test2 了！
```

真是方便那！