樱桃包¶
子包¶
- cherrypy.lib包
- 子模块
- cherrypy.lib.auth_Basic模块
- cherrypy.lib.auth_摘要模块
- cherrypy.lib.缓存模块
- cherrypy.lib.covercp模块
- cherrypy.lib.cpstats模块
- cherrypy.lib.cptools模块
- cherrypy.lib.编码模块
- cherrypy.lib.gctools模块
- cherrypy.lib.httputil模块
- cherrypy.lib.jsontools模块
- cherrypy.lib.锁定模块
- cherrypy.lib.profiler模块
- cherrypy.lib.reprconf模块
- cherrypy.lib.sessions模块
- cherrypy.lib.静态模块
- cherrypy.lib.xmlrpcutil模块
- 模块内容
- 子模块
- cherrypy.工艺包
- 奇瑞皮脚手架包
- Cherrypy.测试包
- 子模块
- cherrypy.test._test_装饰器模块
- cherrypy.test._test_States_demo模块
- cherrypy.test.benchmark模块
- cherrypy.test.checkerdemo模块
- cherrypy.test.helper模块
- cherrypy.test.logtest模块
- cherrypy.test.modfastcgi模块
- cherrypy.test.modfcgid模块
- cherrypy.test.modpy模块
- cherrypy.test.modwsgi模块
- cherrypy.test.sessionDemo模块
- cherrypy.test.test_auth_Basic模块
- cherrypy.test.test_auth_digest模块
- cherrypy.test.test_总线模块
- cherrypy.test.test_缓存模块
- cherrypy.test.test_config模块
- cherrypy.test.test_config_服务器模块
- cherrypy.test.test_conn模块
- cherrypy.test.test_core模块
- cherrypy.test.test_dynamic对象映射模块
- cherrypy.test.test_编码模块
- cherrypy.test.test_eTags模块
- cherrypy.test.test_http模块
- cherrypy.test.test_httputil模块
- cherrypy.test.test_迭代器模块
- cherrypy.test.test_json模块
- cherrypy.test.test_日志记录模块
- cherrypy.test.test_MIME模块
- cherrypy.test.test_misc_tools模块
- cherrypy.test.test_原生模块
- cherrypy.test.test_objectmap模块
- cherrypy.test.test_params模块
- cherrypy.test.test_插件模块
- cherrypy.test.test_Proxy模块
- cherrypy.test.test_refleaks模块
- cherrypy.test.test_request_obj模块
- cherrypy.test.test_routes模块
- cherrypy.test.test_会话模块
- cherrypy.test.test_sessionAuthenticate模块
- cherrypy.test.test_States模块
- cherrypy.test.test_静电模块
- cherrypy.test.test_Tools模块
- cherrypy.test.test_教程模块
- cherrypy.test.test_viralhost模块
- cherrypy.test.test_WSGI_ns模块
- cherrypy.test.test_WSGI_Unix_Socket模块
- cherrypy.test.test_WSGI_vhost模块
- cherrypy.test.test_wsgiapps模块
- cherrypy.test.test_xmlrpc模块
- cherrypy.test.webtest模块
- 模块内容
- 子模块
- cherrypy.tutorial包
- 子模块
- cherrypy.tutorial.tut01_HelloWorld模块
- cherrypy.tutorial.tut02_expose_Methods模块
- cherrypy.tutorial.tut03_GET_和_POST模块
- cherrypy.tutorial.tut04_Complex_Site模块
- cherrypy.tutorial.tut05_派生_对象模块
- cherrypy.tutorial.tut06_Default_Method模块
- cherrypy.tutorial.tut07_会话模块
- cherrypy.tutorial.tut08_generators_and_yield模块
- cherrypy.tutorial.tut09_files模块
- cherrypy.tutorial.tut10_http_Errors模块
- 模块内容
- 子模块
子模块¶
- cherrypy.__main__ 模块
- cherrypy._cpcheckker模块
- cherrypy._cpcompat模块
- cherrypy._cpconfig模块
- cherrypy._cp调度模块
- cherrypy._cperror模块
- cherrypy._日志记录模块
- cherrypy._cpmodpy模块
- cherrypy._cpative_服务器模块
- cherrypy._cpreqbody模块
- cherrypy._cprequest模块
- cherrypy._cpserver模块
- cherrypy._cptools模块
- cherrypy._cptree模块
- cherrypy._cpwsgi模块
- cherrypy._cpwsgi_服务器模块
- Cherrypy._Helper模块
- cherrypy._json模块
- cherrypy.daemon模块
模块内容¶
Cherrypy是一个pythonic的面向对象HTTP框架。
Cherrypy由四个独立的API层组成,而不是一个。
应用层是最简单的。Cherrypy应用程序是以类和方法树的形式编写的,树中的每个分支对应于URL路径中的一个分支。每个方法都是一个“页面处理程序”,它接收get和post参数作为关键字参数,并返回或生成响应的(HTML)主体。特殊方法名“index”用于以斜线结尾的路径,特殊方法名“default”用于通过单个处理程序处理多个路径。该层还包括:
“exposed”属性(和cherrypy.expose)
奇瑞皮(Cherrypy.quickstart)()
_ cp配置属性
cherrypy.tools(包括cherrypy.session)
cherrypy.url()。
环境层由各级开发人员使用。它通过一组顶级对象(默认)提供有关当前请求和响应以及应用程序和服务器环境的信息:
cherrypy.request
cherrypy.response
cherrypy.engine
cherrypy.server
cherrypy.tree
cherrypy.config
cherrypy.thread_data
cherrypy.log
cherrypy.httperror、notfound和httpedirect
cherrypy.lib
扩展层允许高级用户构建和共享自己的插件。它包括:
钩子API
工具API
工具箱API
调度API
配置命名空间API
最后,还有核心层,它使用核心API构建在更高层可用的默认组件。您可以将默认组件视为CherryPy的“引用实现”。MegaFrameworks(和高级用户)可以用自定义或扩展组件替换默认组件。核心API是:
应用程序接口
发动机API
请求API
服务器应用程序接口
WSGi应用程序接口
这些API在 CherryPy specification .
- class cherrypy.Application(root, script_name='', config=None)[源代码]¶
基类:
object
奇瑞派的应用程序。
服务器和网关不应直接实例化请求对象。相反,他们应该向应用程序对象请求请求对象。
此类的一个实例也可以用作自身的wsgi可调用(wsgi应用程序对象)。
- config = {}¶
pathconf pairs,其中“pathconf”本身是key:value pairs的dict。
- 类型
口述路径
- log = None¶
日志管理器实例。请参阅_cplogging。
- namespaces = {}¶
- relative_urls = False¶
- root = None¶
此应用程序页面处理程序的最顶层容器。处理程序应按属性层次结构排列,与预期的URI层次结构相匹配;然后,默认调度程序在此层次结构中搜索匹配的处理程序。使用除默认值以外的调度程序时,此值可能为“无”。
- property script_name¶
此应用程序的URI“安装点”。
安装点是URI的一部分,对于由该应用程序提供服务的所有URI来说,它都是常量;它不包括URI的方案、主机或代理(“虚拟主机”)部分。
例如,如果script_name是“/my/cool/app”,那么URL“http://www.example.com/my/cool/app/page1”可能由根对象上的“page1”方法处理。
脚本名称的值不能以斜线结尾。如果脚本名称引用了URI的根,它必须是空字符串(不是“/”)。
如果script_name显式设置为none,则将为来自request.wsgi_environ的每个调用提供script_name ['SCRIPT_NAME'] .
- script_name_doc = 'The URI "mount point" for this app. A mount point\n is that portion of the URI which is constant for all URIs that are\n serviced by this application; it does not include scheme, host, or proxy\n ("virtual host") portions of the URI.\n\n For example, if script_name is "/my/cool/app", then the URL\n "http://www.example.com/my/cool/app/page1" might be handled by a\n "page1" method on the root object.\n\n The value of script_name MUST NOT end in a slash. If the script_name\n refers to the root of the URI, it MUST be an empty string (not "/").\n\n If script_name is explicitly set to None, then the script_name will be\n provided for each call from request.wsgi_environ[\'SCRIPT_NAME\'].\n '¶
- toolboxes = {'tools': <cherrypy._cptools.Toolbox object>}¶
- wsgiapp = None¶
cpwsgiapp实例。参见_cpwsgi。
- exception cherrypy.HTTPError(status=500, message=None)[源代码]¶
基类:
cherrypy._cperror.CherryPyException
用于向客户端返回HTTP错误代码(4xx-5xx)的异常。
此异常可用于使用HTTP状态代码自动发送响应,并带有适当的错误页。它需要一个可选的
status
参数(必须介于400和599之间);默认为500(“内部服务器错误”)。它还需要一个可选的message
参数,将在响应正文中返回。参见 RFC2616 获取可用错误代码的完整列表以及何时使用它们。实例:
raise cherrypy.HTTPError(403) raise cherrypy.HTTPError( "403 Forbidden", "You are not allowed to access this resource.")
- code = None¶
整数HTTP状态代码。
- reason = None¶
HTTP原因短语字符串。
- set_response()[源代码]¶
修改cherrypy.response status、headers和body以表示自己。
CherryPy在内部使用它,但是您也可以使用它来创建一个httpError对象,并在没有 饲养 例外。
- status = None¶
HTTP状态代码。可以是int或str类型(带有原因短语)。
- exception cherrypy.HTTPRedirect(urls, status=None, encoding=None)[源代码]¶
基类:
cherrypy._cperror.CherryPyException
应重定向请求时引发异常。
此异常将强制HTTP重定向到您提供的URL或URL。必须将新的URL作为第一个参数传递给异常,例如,httpredirect(new url)。列表中允许多个URL。如果URL是绝对的,它将按原样使用。如果它是相对的,则假定它是相对于当前cherrypy.request.path_信息的。
如果提供的URL之一是Unicode对象,则将使用默认编码或传入参数对其进行编码。
有多种类型的重定向,您可以通过
status
参数。如果您不提供status
arg,默认为303(如果使用HTTP/1.0响应,则为302)。实例:
raise cherrypy.HTTPRedirect("") raise cherrypy.HTTPRedirect("/abs/path", 307) raise cherrypy.HTTPRedirect(["path1", "path2?a=1&b=2"], 301)
见 重定向帖子 其他注意事项。
- default_status = 303¶
- encoding = 'utf-8'¶
传递URL时的编码不是本机字符串
- set_response()[源代码]¶
修改cherrypy.response status、headers和body以表示自己。
CherryPy在内部使用它,但是您也可以使用它来创建一个httpRedirect对象,并在没有 饲养 例外。
- property status¶
要发出的整数HTTP状态代码。
- urls = None¶
要发出的URL列表。
- exception cherrypy.InternalRedirect(path, query_string='')[源代码]¶
基类:
cherrypy._cperror.CherryPyException
为切换到其他URL的处理程序而引发异常。
此异常将把处理重定向到站点内的另一个路径(而不通知客户端)。在引发异常时提供新路径作为参数。在查询字符串中为新的URL提供任何参数。
- exception cherrypy.NotFound(path=None)[源代码]¶
基类:
cherrypy._cperror.HTTPError
当URL无法映射到任何处理程序(404)时引发异常。
这相当于提高
HTTPError("404 Not Found")
.
- class cherrypy.Tool(point, callable, name=None, priority=50)[源代码]¶
基类:
object
用于Cherrypy请求处理挂钩的注册函数。
帮助(tool.callable)应该提供有关此工具的更多信息。
- namespace = 'tools'¶
- property on¶
- cherrypy.popargs(*args, **kwargs)[源代码]¶
装饰派工。
(cherrypy.dispatch.dispatcher.dispatch方法名称)
可选关键字参数:handler=(对象或函数)
提供一个_cp_调度函数,该函数将路径段弹出到指定名称下的cherrypy.request.params中。然后将调度转发到下一个vpath元素。
请注意,应用popargs的类的任何现有(和公开)成员函数都将重写该参数的值。例如,如果在用popargs修饰的类上有一个名为“list”的方法,那么访问“/list”将调用该函数,而不是将其作为请求的参数弹出。此限制适用于所有的调度功能。绕过这个限制的唯一方法是创建一个“空白类”,它的唯一功能是提供cp_调度。
如果参数后面有path元素,或者请求的参数多于vpath中可用的参数,那么'handler'关键字参数指定下一个对象来处理参数化的请求。如果未指定处理程序或处理程序为“无”,则使用self。如果处理程序是函数而不是实例,那么将使用指定的参数调用该函数,并且该函数的返回值将用作下一个对象,而不是将参数添加到cherrypy.request.args。
此装饰器可通过以下两种方式之一使用:
作为班级装饰师:
@cherrypy.popargs('year', 'month', 'day') class Blog: def index(self, year=None, month=None, day=None): #Process the parameters here; any url like #/, /2009, /2009/12, or /2009/12/31 #will fill in the appropriate parameters. def create(self): #This link will still be available at /create. #Defined functions take precedence over arguments.
或者作为一个班级的成员:
class Blog: _cp_dispatch = cherrypy.popargs('year', 'month', 'day') #...
处理程序参数可用于将参数与内置函数混合。例如,以下设置允许在日、月和年级别执行不同的活动:
class DayHandler: def index(self, year, month, day): #Do something with this day; probably list entries def delete(self, year, month, day): #Delete all entries for this day @cherrypy.popargs('day', handler=DayHandler()) class MonthHandler: def index(self, year, month): #Do something with this month; probably list entries def delete(self, year, month): #Delete all entries for this month @cherrypy.popargs('month', handler=MonthHandler()) class YearHandler: def index(self, year): #Do something with this year #... @cherrypy.popargs('year', handler=YearHandler()) class Root: def index(self): #...
- cherrypy.quickstart(root=None, script_name='', config=None)[源代码]¶
安装给定的根目录,启动内置服务器(和引擎),然后阻塞。
- 根:“控制器类”(页处理程序的集合)的实例
方法)表示应用程序的根。
- 脚本名称:包含应用程序的“装入点”的字符串。
这应该以斜线开头,并且是URL中装入给定根的路径部分。例如,如果root.index()将处理对“http://www.example.com:8080/dept/app1/”的请求,则脚本“name”参数将为“/dept/app1”。
它不能以斜线结尾。如果脚本名称引用了URI的根,它必须是空字符串(不是“/”)。
- config:包含应用程序配置的文件或dict。如果这个包含
一 [全球的] 部分,这些条目将在全局(站点范围)配置中使用。
- cherrypy.url(path='', qs='', script_name=None, base=None, relative=None)[源代码]¶
为给定路径创建绝对URL。
- 如果“path”以斜线(“/”)开头,则返回
(基础+脚本名称+路径+qs)。
- 如果不是以斜线开头,则返回
(基础+脚本名称 [+ request.path_info] +路径+qs)。
如果script_name为none,则将使用cherrypy.request查找脚本_name(如果可用)。
如果base为none,将使用cherrypy.request.base(如果可用)。请注意,您可以使用cherrypy.tools.proxy来更改这一点。
最后,请注意,此函数可以通过不传递参数来获取当前请求路径(减去querystring)的绝对URL。如果调用url(qs=cherrypy.request.query_string),则应获取原始浏览器url(假定没有内部重定向)。
如果没有或没有提供relative,则将使用request.app.relative_URL(如果可用,则为false)。如果为false,则输出将是绝对URL(包括方案、主机、vhost和脚本名称)。如果为真,则输出将是相对于当前请求路径的URL,可能包括“..”原子。如果relative是字符串“server”,则输出将是相对于服务器根目录的URL;即,它将以斜线开头。