本文整理匯總了Python中mimeparse.best_match方法的典型用法代碼示例。如果您正苦於以下問題:Python mimeparse.best_match方法的具體用法?Python mimeparse.best_match怎麽用?Python mimeparse.best_match使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mimeparse
的用法示例。
在下文中一共展示了mimeparse.best_match方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: determine_content_type
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import best_match [as 別名]
def determine_content_type(self, request):
content_types = [
'application/vnd.siren+json',
'application/vnd.hal+json',
'application/hal+json',
'application/json',
]
accept = request.META.get('HTTP_ACCEPT', '*/*')
try:
content_type = best_match(content_types, accept)
except MimeTypeParseException:
content_type = None
if not content_type:
return content_types[-1]
return content_type
示例2: select_renderer
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import best_match [as 別名]
def select_renderer(request: web.Request, renderers: OrderedDict, force=True):
"""
Given a request, a list of renderers, and the ``force`` configuration
option, return a two-tuple of:
(media type, render callable). Uses mimeparse to find the best media
type match from the ACCEPT header.
"""
header = request.headers.get('ACCEPT', '*/*')
best_match = mimeparse.best_match(renderers.keys(), header)
if not best_match or best_match not in renderers:
if force:
return tuple(renderers.items())[0]
else:
raise web.HTTPNotAcceptable
return best_match, renderers[best_match]
# ###### Renderers ######
# Use a class so that json module is easily override-able
示例3: negotiate
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import best_match [as 別名]
def negotiate(self, request):
request.headers.getall('Accept', "*/*")
mime_type = mimeparse.best_match(['application/json', 'text/event-stream'],
",".join(request.headers.getall('Accept', "*/*")))
if mime_type == 'text/event-stream':
return await self.event_stream(request)
elif mime_type == 'application/json':
return await self.json(request)
else:
raise NotImplementedError(mime_type)
示例4: _best_mime
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import best_match [as 別名]
def _best_mime(supported, accept_string=None):
if accept_string is None:
return None
return mimeparse.best_match(supported, accept_string)
示例5: ui_rest_response_exception_factory
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import best_match [as 別名]
def ui_rest_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
rest_mime_types = list(get_supported_mime_types(get_default_converters()))
ui_mime_types = ['text/html', 'application/xhtml+xml']
best_match = mimeparse.best_match(rest_mime_types + ui_mime_types, request.META.get('HTTP_ACCEPT', 'text/html'))
if best_match in ui_mime_types:
return ui_response_exception_factory(request, response_code, title, message, response_class)
else:
return rest_response_exception_factory(request, response_code, title, message, response_class)