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。
论点是:
name
cookie名称。
value
cookie值,应为字符串或
None
. 如果value
是None
,相当于调用webob.response.Response.unset_cookie()
此cookie键的方法(它有效地删除客户端上的cookie)。max_age
表示秒数的整数,
datetime.timedelta
或None
. 此值用作Max-Age
生成的cookie。如果expires
未传递,而此值未传递None
, themax_age
价值也会影响Expires
cookie的价值 (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
表示
SameSite
cookie的属性或None
. 如果萨米斯是None
不SameSite
值将在cookie中发送。应该只是"strict"
,"lax"
或"none"
.comment
表示cookie的字符串
Comment
价值,或None
.如果comment
是None
没有Comment
值将在cookie中发送。expires
A
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
参数。