pyramid.response¶
- class Response(body=None, status=None, headerlist=None, app_iter=None, content_type=None, conditional_response=None, charset=<object object>, **kw)[源代码]¶
- property accept_ranges¶
获取并设置
Accept-Ranges标题(HTTP规范第14.5节) .
- property age¶
获取并设置
Age标题(HTTP规范第14.6节) . 使用int转换它。
- property allow¶
获取并设置
Allow标题(HTTP规范第14.7节) . 使用列表转换它。
- property app_iter¶
返回
app_iter响应。如果
body已设置,这将创建一个app_iter从那body(单个项目列表)。
- property body_file¶
可用于写入正文的类似文件的对象。如果你通过了一个名单
app_iter,那app_iter将被写入修改。
- property cache_control¶
获取/设置/修改缓存控制头(HTTP规范第14.9节) .
- property charset¶
获取/设置
charset指定在Content-Type.没有检查以验证
content_type实际上允许charset参数。
- conditional_response_app(environ, start_response)[源代码]¶
像正常人一样
__call__接口,但检查条件头:If-Modified-Since(304 Not Modified只有在GET,HEAD)If-None-Match(304 Not Modified只有在GET,HEAD)Range(406 Partial Content只有在GET,HEAD)
- property content_disposition¶
获取并设置
Content-Disposition标题(HTTP规范第19.5.1节) .
- property content_encoding¶
获取并设置
Content-Encoding标题(HTTP规范第14.11节) .
- property content_language¶
获取并设置
Content-Language标题(HTTP规范第14.12节) . 使用列表转换它。
- property content_length¶
获取并设置
Content-Length标题(HTTP规范第14.17节) . 使用int转换它。
- property content_location¶
获取并设置
Content-Location标题(HTTP规范第14.14节) .
- property content_md5¶
获取并设置
Content-MD5标题(HTTP规范第14.14节) .
- property content_range¶
获取并设置
Content-Range标题(HTTP规范第14.16节) . 使用ContentRange对象转换它。
- property content_type¶
获取/设置
Content-Type标题。如果没有Content-Type头已设置,这将返回None.在 1.7 版本发生变更: 设置新的
Content-Type将全部删除Content-Type参数并重置charset如果Content-Type是text/*或XML (application/xml或*/*+xml)保存所有
Content-Type参数,可以使用以下代码:resp = Response() params = resp.content_type_params resp.content_type = 'application/something' resp.content_type_params = params
- property content_type_params¶
内容类型中所有参数的字典。
(这不是视图,设置为“更改”,否则将不应用对dict的修改。)
- property date¶
获取并设置
Date标题(HTTP规范第14.18节) . 使用HTTP日期转换它。
- delete_cookie(name, path='/', domain=None)[源代码]¶
从客户端删除cookie。注意
path和domain必须与最初设置cookie的方式匹配。这会将cookie设置为空字符串,并且
max_age=0以便它立即到期。
- property etag¶
获取并设置
ETag标题(HTTP规范第14.19节) . 使用实体标记转换它。
- property expires¶
获取并设置
Expires标题(HTTP规范第14.21节) . 使用HTTP日期转换它。
- classmethod from_file(fp)[源代码]¶
从类似文件的对象读取响应(它必须实现
.read(size)和.readline())它将一直读到响应的结尾,而不是文件的结尾。
这将读取由表示的响应
str(resp);它可能无法正确读取每个有效的HTTP响应。响应必须具有Content-Length.
- property headerlist¶
响应头列表。
- property headers¶
类似字典的对象中的标题。
- property json¶
将响应的主体设置为/获取为JSON。
- property json_body¶
将响应的主体设置为/获取为JSON。
- property last_modified¶
获取并设置
Last-Modified标题(HTTP规范第14.29节) . 使用HTTP日期转换它。
- property location¶
获取并设置
Location标题(HTTP规范第14.30节) .
- md5_etag(body=None, set_content_md5=False)[源代码]¶
使用主体的MD5哈希为响应对象生成etag
body参数,或self.body如果没有给出)。集合
self.etag.如果
set_content_md5是True集合self.content_md5也。
- merge_cookies(resp)[源代码]¶
将对此响应设置的cookie与给定的
resp对象(可以是任何wsgi应用程序)。如果
resp是一个webob.Response对象,则另一个对象将被就地修改。
- property pragma¶
获取并设置
Pragma标题(HTTP规范第14.32节) .
- property retry_after¶
获取并设置
Retry-After标题(HTTP规范第14.37节) . 使用HTTP日期或增量秒转换它。
- property server¶
获取并设置
Server标题(HTTP规范第14.38节) .
- set_cookie(name, value='', max_age=None, path='/', domain=None, secure=False, httponly=False, comment=None, expires=None, overwrite=False, samesite=None)[源代码]¶
为响应设置(添加)cookie。
论点是:
namecookie名称。
valuecookie值,应为字符串或
None. 如果value是None,相当于调用webob.response.Response.unset_cookie()此cookie键的方法(它有效地删除客户端上的cookie)。max_age表示秒数的整数,
datetime.timedelta或None. 此值用作Max-Age生成的cookie。如果expires未传递,而此值未传递None, themax_age价值也会影响Expirescookie的价值 (Expires将被设置为now+max_age)如果该值为None,cookie将没有Max-Age价值(除非)expires设置)。如果两者max_age和expires设置后,此值优先。path表示cookie的字符串
Path价值。它默认为/.domain表示cookie的字符串
Domain或None. 如果域是None没有Domain值将在cookie中发送。secure布尔值。如果是
True, thesecure标志将在cookie中发送,如果False, thesecure标志不会在cookie中发送。httponly布尔值。如果是
True, theHttpOnly标志将在cookie中发送,如果False, theHttpOnly标志不会在cookie中发送。samesite表示
SameSitecookie的属性或None. 如果萨米斯是None不SameSite值将在cookie中发送。应该只是"strict","lax"或"none".comment表示cookie的字符串
Comment价值,或None.如果comment是None没有Comment值将在cookie中发送。expiresA
datetime.timedelta表示一段时间的对象,datetime.datetime或None. 使用非“none”值生成Expires生成的cookie的值。如果max_age未传递,但此值不是None,它将影响Max-Age标题。如果该值为None, theExpires将取消设置cookie值(除非max_age设置)。如果max_age设置,它将用于生成expires这个值被忽略。如果A
datetime.datetime如果提供,则必须知道时区或基于UTC。datetime.datetime不支持本地时间的对象。时区感知datetime.datetime对象转换为UTC。此参数将在未来版本的WebOB(1.9版)中删除。
overwrite如果这个钥匙是
True,在设置cookie之前,请取消设置任何现有的cookie。
- property status¶
状态字符串。
- property status_code¶
作为整数的状态。
- property status_int¶
作为整数的状态。
- property text¶
使用获取/设置正文的文本值
charset的Content-Type或default_body_encoding.
- property ubody¶
.text的别名已弃用
- property unicode_body¶
.text的别名已弃用
- property vary¶
获取并设置
Vary标题(HTTP规范第14.44节) . 使用列表转换它。
- property www_authenticate¶
获取并设置
WWW-Authenticate标题(HTTP规范第14.47节) . 使用转换它parse_auth和serialize_auth.
- class FileResponse(path, request=None, cache_max_age=None, content_type=None, content_encoding=None)[源代码]¶
一种响应对象,可用于简单地从磁盘为静态文件提供服务。
path是磁盘上的文件路径。request一定是Pyramid request 对象。请注意,请求 must 如果响应试图使用wsgi.file_wrapper用于为Pyramid应用程序提供服务的Web服务器的功能。cache_max_age是用于HTTP缓存此响应的秒数。content_type是响应的内容类型。content_encoding是响应的内容编码。把这套衣服放在None如果您提供的是二进制文件。如果您也离开,此参数将被忽略content-type作为None.
- class FileIter(file, block_size=262144)[源代码]¶
用于wsgi应用程序的固定块大小迭代器。
file是python文件指针(或至少是具有read获取大小提示的方法)。block_size是迭代的可选块大小。
功能¶
- response_adapter(*types_or_ifaces, **kwargs)[源代码]¶
通过激活装饰器 scan 它将被装饰的功能视为 response adapter 对于作为
*types_or_ifaces到decorator构造函数。例如,如果扫描以下响应适配器:
from pyramid.response import Response from pyramid.response import response_adapter @response_adapter(int) def myadapter(i): return Response(status=i)
然后,您可以从视图可调用文件中返回一个整数,并将其转换为以该整数作为状态代码的响应。
可以将多个类型或接口作为构造函数参数传递。将为每个类型或接口调用经过修饰的响应适配器。
import json from pyramid.response import Response from pyramid.response import response_adapter @response_adapter(dict, list) def myadapter(ob): return Response(json.dumps(ob))
此方法在 scan 对包含它的包或模块执行,ala:
from pyramid.config import Configurator config = Configurator() config.scan('somepackage_containing_adapters')
将传递给 venusian
attach函数是_depth和_category._depth是为希望从另一个装饰器重用此类的人提供的。默认值为0并且应该相对于response_adapter调用。它将传递给 venusianattach当Venusian检查装饰器是否在类或模块上下文中使用时,函数作为调用堆栈的深度。它不常用,但在这种情况下可能有用。_category设置装饰器类别名称。它可以与category的参数scan控制应处理哪些视图。见
venusian.attach()在金星函数中获取更多关于_depth和_category参数。在 1.9.1 版本发生变更: 增加了
_depth和_category参数。