當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。