Package: xtractmime
xtractmimextractmimeis aBSD-licensedPython 3.7+ implementation of theMIME Sniffing
Standard.Install fromPyPI:pip install xtractmimeBasic usageBelow mentioned are some simple examples of usingxtractmime.extract_mime:>>>fromxtractmimeimportextract_mime>>>extract_mime(b'Sample text content')b'text/plain'>>>extract_mime(b'',content_types=(b'text/html',))b'text/html'Additional functionality to check if a MIME type belongs to a specific MIME type group using
methods included inxtractmime.mimegroups:>>>fromxtractmime.mimegroupsimportis_html_mime_type,is_image_mime_type>>>mime_type=b'text/html'>>>is_html_mime_type(mime_type)True>>>is_image_mime_type(mime_type)FalseAPI Referencefunctionxtractmime.extract_mime(*args, **kwargs) -> Optional[bytes]Parameters:body: bytescontent_types: Optional[Tuple[bytes]] = Nonehttp_origin: bool = Trueno_sniff: bool = Falseextra_types: Optional[Tuple[Tuple[bytes, bytes, Optional[Set[bytes]], bytes], ...]] = Nonesupported_types: Set[bytes] = NoneReturn theMIME type essence(e.g.text/html) matching the input data, orNoneif no match can be found.Thebodyparameter is the byte sequence of which MIME type is to be determined.xtractmimeonly considers the first few
bytes of thebodyand the specific number of bytes read is defined in thextractmime.RESOURCE_HEADER_BUFFER_LENGTHconstant.content_typesis a tuple of MIME types given in the resource metadata. For example, for resources retrieved via HTTP, users should pass the list of MIME types mentioned in theContent-Typeheader.http_originindicates if the resource has been retrieved via HTTP (True, default) or not (False).no_sniffis a flag which isTrueif the user agent does not wish to
perform sniffing on the resource andFalse(by default) otherwise. Users may want to set
this parameter toTrueif theX-Content-Type-Optionsresponse header is set tonosniff. For more info, seehere.extra_typesis a tuple of patterns to support detecting additional MIME types. Each entry in the tuple should follow the format(Byte Pattern, Pattern Mask, Leading Bytes, MIME type):Byte Patternis a byte sequence to compare with the first few bytes (xtractmime.RESOURCE_HEADER_BUFFER_LENGTH) of thebody.Pattern Maskis a byte sequence that indicates the significance ofByte Patternbytes:b"\xff"indicates the matching byte is strictly significant,b"\xdf"indicates that the byte is significant in an ASCII case-insensitive way, andb"\x00"indicates that the byte is not significant.Leading Bytesis a set of bytes to be ignored while matching the leading bytes in the content.MIME typeshould be returned if the pattern matches.Sampleextra_types:extra_types=((b'test',b'\xff\xff\xff\xff',None,b'text/test'),...)NOTEBe careful while using theextra_typesargument, as it may introduce some privilege escalation vulnerabilities forxtractmime. For more info, seehere.Optionalsupported_typesis a set of allMIME types supported the by user agent. Ifsupported_typesis not
specified, all MIME types are assumed to be supported. Using this parameter can improve the performance ofxtractmime.functionxtractmime.is_binary_data(input_bytes: bytes) -> boolReturnTrueif the provided byte sequence contains any binary data bytes, elseFalseMIME type group functionsThe following functions returnTrueif a given MIME type belongs to a certainMIME type group, orFalseotherwise:xtractmime.mimegroups.is_archive_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_audio_video_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_font_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_html_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_image_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_javascript_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_json_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_scriptable_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_xml_mime_type(mime_type: bytes) -> bool
xtractmime.mimegroups.is_zip_mime_type(mime_type: bytes) -> boolExample>>>fromxtractmime.mimegroupsimportis_html_mime_type,is_image_mime_type,is_zip_mime_type>>>mime_type=b'text/html'>>>is_html_mime_type(mime_type)True>>>is_image_mime_type(mime_type)False>>>is_zip_mime_type(mime_type)FalseChangelogSee thechangelog