pythonic cherrypy

刚才看到这个页面:http://tools.cherrypy.org/wiki/InteractiveInterpreter,发现 cherrypy 还可以这样做,真是有点意思,正如作者所说: We think it showcases the pythonic nature of 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 了!

真是方便那!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值