cherrypy安装使用,配置python环境变量 CherryPy是一种基于Python的Web应用程序开发框架,一个相当不错的HTTP Framework,它极大地简化了使用Python 的web开发人员的工作。1.安装cherrypy:从下载最新的cherrypy包解压缩后到目录下运行安装目录(注:必须先安装python并把python目录加到环境变量中)C:\Documents and Settings\zheng>G:G:\>cd sns/CherryPy-3.1.2G:\sns\CherryPy-3.1.2>python setup.py buildG:\sns\CherryPy-3.1.2>python setup.py install安装完毕就可以用了。2.在G:\sns\CherryPy-3.1.2目录下创建一个hello.py的文件,输入下面简单的几行代码可以实现一个“Hello, World!”Application import cherrypy class HelloWorld(object): def index(self): return "Hello World!" index.exposed = Truecherrypy.quickstart(HelloWorld()) 3.回到命令行输入下面命令启动 G:\sns\CherryPy-3.1.2>python hello.py[16/Jan/2011:21:35:07] ENGINE Listening for SIGTERM.[16/Jan/2011:21:35:07] ENGINE Bus STARTINGCherryPy Checker:The Application mounted at '' has an empty config.[16/Jan/2011:21:35:07] ENGINE Started monitor thread 'Autoreloader'. [16/Jan/2011:21:35:07] ENGINE Started monitor thread '_TimeoutMonitor'.[16/Jan/2011:21:35:08] ENGINE Serving on 127.0.0.1:8080[16/Jan/2011:21:35:08] ENGINE Bus STARTED4.在浏览器输入 访问可以看到下面内容: Hello World!输入也访问到一样的内容5.把上面内容改为 import cherrypyclass HelloWorld(object): def hello(self): return "hello" def index(self): return "Hello World!" hello.exposed = True index.exposed = Truecherrypy.quickstart(HelloWorld()) 在浏览器中输入可以看到输出为hello====================================== 配置python环境变量 G:\sns\CherryPy-3.1.2>python'python' 不是内部或外部命令,也不是可运行的程序或批处理文件。默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量。此时不能在命令行直接使用python命令。 1.首先需要在系统中注册python环境变量:假设python的安装路径为c:\python2.5,则修改我的电脑->属性->高级->环境变量->系统变量中的PATH为:PATH=PATH;c:\python25 上述环境变量设置成功之后,就可以在命令行直接使用python命令。或执行"python *.py"运行python脚本了。 2.此时,还是只能通过"python *.py"运行python脚本,若希望直接运行*.py,只需再修改另一个环境变量PATHEXT: PATHEXT=PATHEXT;.PY;.PYM 3.另外,在使用python的过程中,可能需要经常查看某个命令的帮助文档,如使用help('print')查看print命令的使用说明。默认安装的python无法查看帮助文档,尚需进行简单的配置: 在python安装目录下,找到python25.chm,使用hh -decompile .python25.chm将其反编译出来,然后将其所在的目录加入到上面提到的PATH环境变量中即可。 ================================================ 使用PyWin32将CherryPy应用安装为服务 # coding=cp936 import cherrypyimport win32serviceutilimport win32serviceimport win32eventclass HelloWorld:""" 请求句柄例子 """# 暴露对象@cherrypy.exposedef index(self):# 只做测试用,所以只返回简单内容return "Hello world!"class MyService(win32serviceutil.ServiceFramework):"""NT 服务"""# 服务名_svc_name_ = "CherryPyService"# 服务显示名称_svc_display_name_ = "CherryPy Service"def __init__(self, args):win32serviceutil.ServiceFramework.__init__(self, args)# 创建一个事件self.stop_event = win32event.CreateEvent(None, 0, 0, None)def SvcDoRun(self):cherrypy.root = HelloWorld()# 更新配置,当使用配置文件时,必须为一个绝对路径cherrypy.config.update({ 'global':{ 'server.socketPort' : 81,'server.environment': 'production','server.logToScreen': False,'server.log_file': 'e:editlog.txt','server.log_access_file': 'e:editlog.txt','autoreload.on' : False,'server.logTracebacks' : True} })# 设置 initOnly=Truecherrypy.server.start(initOnly=True)win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)def SvcStop(self):self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)cherrypy.server.stop()win32event.SetEvent(self.stop_event)if __name__ == '__main__':win32serviceutil.HandleCommandLine(MyService)P.S.假设文件名为mysrv.py 则 x:>mysrv.py --显示所有可用命令选项 mysrv.py install--安装服务 mysrv.py start --启动服务 mysrv.py stop --停止服务 mysrv.py restart --就是restart,重启,更新代码用 mysrv.py remove --删除服务 再P.S.测试时候一定要注意防火墙,服务是自动拦截的(pythonservice.exe).偶就在这里出问题了~嘻嘻 用到的路径必须全为绝对路径,静态目录也是 安装好服务后,也可以使用net start/stop CherryPyService 来管理
|