本文整理汇总了Python中magic.MAGIC_MIME_TYPE属性的典型用法代码示例。如果您正苦于以下问题:Python magic.MAGIC_MIME_TYPE属性的具体用法?Python magic.MAGIC_MIME_TYPE怎么用?Python magic.MAGIC_MIME_TYPE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类magic
的用法示例。
在下文中一共展示了magic.MAGIC_MIME_TYPE属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _try_magic
# 需要导入模块: import magic [as 别名]
# 或者: from magic import MAGIC_MIME_TYPE [as 别名]
def _try_magic(self):
try:
import magic
import weakref
except ImportError:
self._magic = None
else:
try:
_magic = magic.Magic(flags=magic.MAGIC_MIME_TYPE)
def cleanup(x):
_magic.close()
self._magic_weakref = weakref.ref(self, cleanup)
self._magic = _magic
except TypeError:
self._magic = None
except AttributeError:
self._magic = None
示例2: unzip_content
# 需要导入模块: import magic [as 别名]
# 或者: from magic import MAGIC_MIME_TYPE [as 别名]
def unzip_content(r, *args, **kwargs):
content = r.content
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
mime = m.id_buffer(content)
if mime == "application/zip":
zip_buffer = io.BytesIO(content)
with zipfile.ZipFile(zip_buffer) as zf:
fn = zf.namelist()[0]
with zf.open(fn) as f:
r._content = f.read()
elif mime == "application/x-gzip":
gz_buffer = io.BytesIO(content)
with gzip.GzipFile(fileobj=gz_buffer) as gz:
r._content = gz.read()
else:
r._content = content
return r
示例3: scan
# 需要导入模块: import magic [as 别名]
# 或者: from magic import MAGIC_MIME_TYPE [as 别名]
def scan(self, payload: Payload, request: Request) -> WorkerResponse:
if USE_PYTHON_MAGIC:
magic_scan = magic.Magic(mime=True)
magic_result = magic_scan.from_buffer(payload.content[0:1000])
else:
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
magic_result = m.id_buffer(payload.content[0:1000])
if hasattr(magic_result, 'decode'):
magic_result = magic_result.decode('utf-8')
return WorkerResponse(results={'mimetype': magic_result})