本文整理匯總了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})