本文整理汇总了Python中mimetypes.MimeTypes方法的典型用法代码示例。如果您正苦于以下问题:Python mimetypes.MimeTypes方法的具体用法?Python mimetypes.MimeTypes怎么用?Python mimetypes.MimeTypes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mimetypes
的用法示例。
在下文中一共展示了mimetypes.MimeTypes方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def __init__(self,
local_directory,
view_name,
url_token='filename',
show_timestamp=False,
width=70):
"""
Args:
local_directory: The local file system path that will be served.
view_name: The name of the app view that maps to the directory.
url_token: The path will be inserted into this token in the URL.
show_timestamp: Include the last accessed timestamp for each file.
width: The page width to use when formatting timestamp strings.
"""
self.local_directory = local_directory
self.view_name = view_name
self.url_token = url_token
self.show_timestamp = show_timestamp
self.width = width
# Custom file extensions can be added via self.mimetypes.add_type()
self.mimetypes = mimetypes.MimeTypes()
示例2: do_GET
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def do_GET(self):
parts = urlparse(self.path)
path = parts.path.strip('/')
if path == "":
path = "index.htm"
file = os.path.join("./UI/", path)
mime = mimetypes.MimeTypes().guess_type(file)[0]
self.send_response(200)
# self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-Type", mime)
self.end_headers()
with open(file, "rb") as f:
self.wfile.write(f.read())
示例3: video_tag
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def video_tag(self, attrs=None):
if attrs is None:
attrs = {}
else:
attrs = attrs.copy()
if self.thumbnail:
attrs['poster'] = self.thumbnail.url
transcodes = self.transcodes.exclude(processing=True).filter(error_message__exact='')
sources = []
for transcode in transcodes:
sources.append("<source src='{0}' type='video/{1}' >".format(transcode.url, transcode.media_format.name))
mime = mimetypes.MimeTypes()
sources.append("<source src='{0}' type='{1}'>"
.format(self.url, mime.guess_type(self.url)[0]))
sources.append("<p>Sorry, your browser doesn't support playback for this video</p>")
return mark_safe(
"<video {0}>\n{1}\n</video>".format(flatatt(attrs), "\n".join(sources)))
示例4: builtin_mimetypes
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def builtin_mimetypes(func):
@wraps(func)
async def decorated(*args, **kwargs):
with patch.object(utils, 'magic') as magic:
magic.__bool__.return_value = False
with patch.object(utils, 'mime') as mime:
mime.guess_type.side_effect = mimetypes.MimeTypes().guess_type
await func(*args, **kwargs)
return decorated
示例5: setUp
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def setUp(self):
self.db = mimetypes.MimeTypes()
示例6: test_registry_parsing
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def test_registry_parsing(self):
# the original, minimum contents of the MIME database in the
# Windows registry is undocumented AFAIK.
# Use file types that should *always* exist:
eq = self.assertEqual
mimetypes.init()
db = mimetypes.MimeTypes()
eq(db.guess_type("foo.txt"), ("text/plain", None))
eq(db.guess_type("image.jpg"), ("image/jpeg", None))
eq(db.guess_type("image.png"), ("image/png", None))
示例7: __init__
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def __init__(self, server):
Module.__init__(self, server)
self.dispatcher = Dispatcher(self.scheduler)
self.mimetypedatabase = mimetypes.MimeTypes(self.mimetypes)
self._cache = {}
self.apiroutine = RoutineContainer(self.scheduler)
self.lastcleartime = 0
def start(asyncStart = False):
self._createHandlers(self)
def close():
self.dispatcher.close()
self.apiroutine.start = start
self.apiroutine.close = close
self.routines.append(self.apiroutine)
self.createAPI(api(self.updateconfig))
示例8: assets_set_asset
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def assets_set_asset(self, asset_name, file, **kwargs):
"""Set an asset image by name."""
content_type = mimetypes.MimeTypes().guess_type(file)
files = {
asset_name: (file, open(file, 'rb'), content_type[0], {'Expires': '0'}),
}
return self.__call_api_post('assets.setAsset', kwargs=kwargs, use_json=False, files=files)
示例9: test_encoding
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def test_encoding(self):
getpreferredencoding = locale.getpreferredencoding
self.addCleanup(setattr, locale, 'getpreferredencoding',
getpreferredencoding)
locale.getpreferredencoding = lambda: 'ascii'
filename = support.findfile("mime.types")
mimes = mimetypes.MimeTypes([filename])
exts = mimes.guess_all_extensions('application/vnd.geocube+xml',
strict=True)
self.assertEqual(exts, ['.g3', '.g\xb3'])
示例10: __init__
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def __init__(self):
self.classes = {}
self.mimetypes = MimeTypes()
mimedata = get_data('scrapy', 'mime.types').decode('utf8')
self.mimetypes.readfp(StringIO(mimedata))
for mimetype, cls in six.iteritems(self.CLASSES):
self.classes[mimetype] = load_object(cls)
示例11: mime_type
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def mime_type(self):
if not self._mime_type:
if self.abspath.endswith(".js"):
self._mime_type = "application/javascript"
elif self.abspath.endswith(".css"):
self._mime_type = "text/css"
elif self.abspath.endswith(".json"):
self._mime_type = mimetype.JSON
else:
mime = MimeTypes()
self._mime_type, _ = mime.guess_type(self.abspath)
if not self._mime_type:
self._mime_type = "application/binary"
return self._mime_type
示例12: open_files
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import MimeTypes [as 别名]
def open_files(self):
self.close_files()
files = []
for x, file_or_name in enumerate(self.paths):
name = ''
if isinstance(file_or_name, tuple) and len(file_or_name) >= 2:
name = file_or_name[1]
file_or_name = file_or_name[0]
if hasattr(file_or_name, 'read'):
f = file_or_name
if hasattr(f, 'name'):
filename = f.name
else:
filename = name
else:
filename = file_or_name
f = open(filename, 'rb')
self.opened_files.append(f)
mimetype = mimetypes.MimeTypes().guess_type(filename)[0]
files.append(
(self.key_format.format(x), ('file{}'.format(x), f, mimetype))
)
return files