cherrypy.root.admin.user("8173","schedule") class Admin: def user(self,*args): if not args: #空则抛出HTTP错误 raise cherrypy.HTTPError(400,"Error") args=list(args) #args是tuple,需要转为list id=args.pop(0) if args and args[0]=="schedule": return self.schedule(id) return "You asked for user '%s'"%id user.exposed=TrueCherryPy2.1中的位置参数只允许出现在default方法中。
CherryPy路径搜索顺序
"/admin/user/8192/schedule" 试搜索cherrypy.root.admin.user.8192.schedule.index... cherrypy.root 存在? Yes. .root.admin 存在? Yes. .admin.user 存在? Yes. .user.8192 存在? No. .user.default 是否可调用+暴露? No. .admin.user 是否可调用+暴露? Yes.调用它.
"/admin/search/" 试搜索cherrypy.root.admin.search.index... cherrypy.root 存在? Yes. .root.admin 存在? Yes. .admin.search 存在? Yes. .search.index 存在? Yes.路径到达. .search.index 是否可调用+暴露? Yes.调用它.
"/admin/unknown" 试搜索cherrypy.root.admin.unknown.index... cherrypy.root 存在? Yes. .root.admin 存在? Yes. .admin.unknown 存在? No. .admin.default 是否可调用+暴露? No. .root.admin 是否可调用+暴露? No. .root.default 是否可调用+暴露? Yes.调用它.
CherryPy主要核心设置选项 [global] server.socket_port 监听端口,默认8080 [global] server.log_file 记录日志,默认关闭 [global] server.log_access_file 存储访问日志,默认是显示到屏幕上 [global] server.log_to_screen 将日志显示到屏幕,默认为True,发布站点最好用False并写入文件 [global] server.log_tracebacks 将跟踪信息写入日志,默认为True。False时只写入500错误 [global] server.max_request_header_size 最大header请求大小(bytes),默认500KB,超过了返回403错误,全局设置,0为不限制。 [global] server.default_content_type 返回信息的缺省类型,默认text/html,(by gashero)全局的 [/path] server.max_request_body_size 最大body请求大小(bytes),默认为100MB。如果请求大于此数字,返回413错误。限制用户上传文件大小的好办法。可以具体到目录。0为不限制。
暴露对象和方法 方法 hello 和 index 上的 装饰符 @cpg.expose 有什么用呢?它告诉 CherryPy,调用该方法来响应 Web 请求没有问题。 在提供静态文件的 Web 网站,假定 Web 空间中的每个文件和目录都计划用于公共消耗。但是也有几处例外,例如,Apache 就不会提供隐藏的文件,如 UNIX® 系统上的 .htaccess。 当您将一个对象树暴露为一个 CherryPy URL 空间时,装饰符是将方法暴露给 CherryPy 的最佳方式,但是也可以通过将其成员 exposed 设置为 True 来暴露一个方法
#@cherrypy.expose class HelloWorld: def index(self): return "Hello, world!" index.exposed = True
挂载对象空间树
cherrypy.quickstart(FileDemo(), config=tutconf) or cherrypy.tree.mount(FileDemo(), config=tutconf) or cherrypy.cpg.root = FileDemo() cherrypy.server.start()