本文整理匯總了Python中mimeparse.parse_mime_type方法的典型用法代碼示例。如果您正苦於以下問題:Python mimeparse.parse_mime_type方法的具體用法?Python mimeparse.parse_mime_type怎麽用?Python mimeparse.parse_mime_type使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mimeparse
的用法示例。
在下文中一共展示了mimeparse.parse_mime_type方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: should_compress_body
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import parse_mime_type [as 別名]
def should_compress_body(response):
'''
Returns true if the response body should be compressed.
This logic can be amended over time to add additional MIME types that
should be compressed.
:param starbelly.downloader.DownloadResponse response:
'''
should_compress = False
type_, subtype, _ = mimeparse.parse_mime_type(response.content_type)
if type_ == 'text':
should_compress = True
elif type_ == 'application' and subtype in ('json', 'pdf'):
should_compress = True
return should_compress
示例2: is_text_content
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import parse_mime_type [as 別名]
def is_text_content(mimetype):
try:
maintype, subtype, _ = parse_mime_type(mimetype)
maintype = maintype.lower()
if maintype == "text":
return True
if maintype == "application":
return subtype.rpartition("+")[-1].lower() in ("xml", "json")
except Exception as e:
logger.warn("Failed to parse mimetype '%s': %s", mimetype, e)
return False
示例3: require_representation
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import parse_mime_type [as 別名]
def require_representation(self, req):
"""Require raw representation dictionary from falcon request object.
This does not perform any field parsing or validation but only uses
allowed content-encoding handler to decode content body.
Note:
Currently only JSON is allowed as content type.
Args:
req (falcon.Request): request object
Returns:
dict: raw dictionary of representation supplied in request body
"""
try:
type_, subtype, _ = parse_mime_type(req.content_type)
content_type = '/'.join((type_, subtype))
except:
raise falcon.HTTPUnsupportedMediaType(
description="Invalid Content-Type header: {}".format(
req.content_type
)
)
if content_type == 'application/json':
body = req.stream.read()
return json.loads(body.decode('utf-8'))
else:
raise falcon.HTTPUnsupportedMediaType(
description="only JSON supported, got: {}".format(content_type)
)
示例4: extract_urls
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import parse_mime_type [as 別名]
def extract_urls(response):
'''
Extract URLs from a response body.
Any relative URLs found in the response body are converted to absolute URLs
using the original request URL.
:param starbelly.downloader.DownloadResponse response: A response to
extract URLs from.
:returns: A list of URLs.
:rtype: list[str]
'''
extracted_urls = list()
if response.is_success:
base_url = response.url
type_, subtype, _ = mimeparse.parse_mime_type(response.content_type)
if type_ == 'text' and subtype == 'html' or \
type_ == 'application' and subtype == 'xhtml+xml':
extracted_urls = _extract_html(response)
elif type_ == 'application' and subtype == 'atom+xml' or \
type_ == 'application' and subtype == 'rss+xml':
extracted_urls = _extract_feed(response)
else:
raise ValueError('Unsupported MIME in extract_urls(): {} (url={})'
.format(response.content_type, base_url))
return extracted_urls
示例5: status
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import parse_mime_type [as 別名]
def status(self, test_id=None, test_status=None, test_tags=None,
runnable=True, file_name=None, file_bytes=None, eof=False,
mime_type=None, route_code=None, timestamp=None):
super(Starts, self).status(
test_id, test_status,
test_tags=test_tags, runnable=runnable, file_name=file_name,
file_bytes=file_bytes, eof=eof, mime_type=mime_type,
route_code=route_code, timestamp=timestamp)
if not test_id:
if not file_bytes:
return
if not mime_type or mime_type == 'test/plain;charset=utf8':
mime_type = 'text/plain; charset=utf-8'
primary, sub, parameters = mimeparse.parse_mime_type(mime_type)
content_type = testtools.content_type.ContentType(
primary, sub, parameters)
content = testtools.content.Content(
content_type, lambda: [file_bytes])
text = content.as_text()
if text and text[-1] not in '\r\n':
self._neednewline = True
self._output.write(text)
elif test_status == 'inprogress' and test_id not in self._emitted:
if self._neednewline:
self._neednewline = False
self._output.write('\n')
worker = ''
for tag in test_tags or ():
if tag.startswith('worker-'):
worker = '(' + tag[7:] + ') '
if timestamp:
timestr = timestamp.isoformat()
else:
timestr = ''
self._output.write('%s: %s%s [start]\n' %
(timestr, worker, test_id))
self._emitted.add(test_id)
示例6: __init__
# 需要導入模塊: import mimeparse [as 別名]
# 或者: from mimeparse import parse_mime_type [as 別名]
def __init__(self, http, postproc, uri,
method='GET',
body=None,
headers=None,
methodId=None,
resumable=None):
"""Constructor for an HttpRequest.
Args:
http: httplib2.Http, the transport object to use to make a request
postproc: callable, called on the HTTP response and content to transform
it into a data object before returning, or raising an exception
on an error.
uri: string, the absolute URI to send the request to
method: string, the HTTP method to use
body: string, the request body of the HTTP request,
headers: dict, the HTTP request headers
methodId: string, a unique identifier for the API method being called.
resumable: MediaUpload, None if this is not a resumbale request.
"""
self.uri = uri
self.method = method
self.body = body
self.headers = headers or {}
self.methodId = methodId
self.http = http
self.postproc = postproc
self.resumable = resumable
self.response_callbacks = []
self._in_error_state = False
# Pull the multipart boundary out of the content-type header.
major, minor, params = mimeparse.parse_mime_type(
headers.get('content-type', 'application/json'))
# The size of the non-media part of the request.
self.body_size = len(self.body or '')
# The resumable URI to send chunks to.
self.resumable_uri = None
# The bytes that have been uploaded.
self.resumable_progress = 0
# Stubs for testing.
self._rand = random.random
self._sleep = time.sleep