CherryPy 中的 Hello 世界
如果你有 virtualenv 并且已经安装了 CherryPy,请创建一个文件 hello.py
:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cherrypy
class HelloWorld(object):
@cherrypy.expose
def index(self):
return 'Hello World!'
@cherrypy.expose
def greet(self, name):
return 'Hello {}!'.format(name)
cherrypy.quickstart(HelloWorld())
然后执行文件:$ hello.py
或 $ python hello.py
。你应该看到与此类似的输出:
user@computer /develop/myapp $ python hello.py
[06/Nov/2016:05:58:44] ENGINE Listening for SIGTERM.
[06/Nov/2016:05:58:44] ENGINE Bus STARTING
[06/Nov/2016:05:58:44] ENGINE Set handler for console events.
CherryPy Checker:
The Application mounted at '' has an empty config.
[06/Nov/2016:05:58:44] ENGINE Started monitor thread '_TimeoutMonitor'.
[06/Nov/2016:05:58:44] ENGINE Started monitor thread 'Autoreloader'.
[06/Nov/2016:05:58:45] ENGINE Serving on http://127.0.0.1:8080
[06/Nov/2016:05:58:45] ENGINE Bus STARTED
- 看’Hello World!’ 将浏览器指向 http:// localhost:8080 /
- 要查看问候语,请转到 http:// localhost:8080 / greet?name = John