本文整理匯總了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