当前位置: 首页>>代码示例>>Python>>正文


Python mimeparse.best_match方法代码示例

本文整理汇总了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 
开发者ID:apiaryio,项目名称:polls-api,代码行数:21,代码来源:resource.py

示例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 
开发者ID:sloria,项目名称:aiohttp-utils,代码行数:22,代码来源:negotiation.py

示例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) 
开发者ID:mrphlip,项目名称:lrrbot,代码行数:12,代码来源:eventserver.py

示例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) 
开发者ID:yfauser,项目名称:planespotter,代码行数:6,代码来源:mimerender.py

示例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) 
开发者ID:matllubos,项目名称:django-is-core,代码行数:12,代码来源:response.py


注:本文中的mimeparse.best_match方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。