本文整理汇总了Python中wheezy.http.HTTPResponse.buffer方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse.buffer方法的具体用法?Python HTTPResponse.buffer怎么用?Python HTTPResponse.buffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wheezy.http.HTTPResponse
的用法示例。
在下文中一共展示了HTTPResponse.buffer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process
# 需要导入模块: from wheezy.http import HTTPResponse [as 别名]
# 或者: from wheezy.http.HTTPResponse import buffer [as 别名]
def process(self, url=None, method='GET', data=None, environ=None, secure=False,
content_type='html', route_name=None, encoding='UTF-8'):
content_type_to_http_protocol_content_types = {
'html': 'text/html; charset={}'.format(encoding),
'json': 'application/json; charset={}'.format(encoding)
}
original_content_type = content_type
content_type = content_type_to_http_protocol_content_types[content_type]
if route_name:
url = router.path_for(route_name)
if url is None:
raise ValueError('Please provide correct url for request or proper route_name')
data = data or {}
method = method.upper()
response = HTTPResponse()
r = self._base_environ()
r.update({
'PATH_INFO': url,
'REQUEST_METHOD': str(method),
'SERVER_PORT': str('443') if secure else str('80'),
'wsgi.url_scheme': str('https') if secure else str('http'),
})
if method in ('POST', 'PUT'):
formatter = get_formatter_for(original_content_type)
data = formatter.format(data).encode(encoding)
r.update({
'CONTENT_LENGTH': len(data),
'CONTENT_TYPE': str(content_type),
'wsgi.input': FakePayload(data),
})
elif method == 'GET':
# WSGI requires latin-1 encoded strings.
r['QUERY_STRING'] = bytes(parse.urlencode(data), 'iso-8859-1').decode()
r.update(environ or {})
def wsgi_response_handler(response, status_string, headers):
response.content_type = [header[1] for header in headers if header[0].lower() == 'content-type'].pop()
response.status_code = HTTP_STATUS_STRING_TO_CODE[status_string]
response.headers = headers
output = application(r, partial(wsgi_response_handler, response))
response.buffer = output
return response