{"id":24033,"library":"mimesniff","title":"mimesniff","description":"A pure Python implementation of the MIME sniffing algorithm from the WHATWG MIME Sniffing specification. Version 1.1.1 supports Python >=3.4 and allows sniffing MIME types from file-like objects or pre-read headers. Uses a decision tree based on byte patterns. Released infrequently.","status":"active","version":"1.1.1","language":"python","source_language":"en","source_url":"https://github.com/corona10/mimesniff","tags":["mime","sniffing","whatwg","python"],"install":[{"cmd":"pip install mimesniff","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"","wrong":"","symbol":"MimeSniffer","correct":"from mimesniff import MimeSniffer"},{"note":"sniff is a top-level function in mimesniff, not a submodule.","wrong":"from mimesniff.sniffer import sniff","symbol":"sniff","correct":"from mimesniff import sniff"}],"quickstart":{"code":"import io\nfrom mimesniff import sniff\n\n# Example from bytes\nmime_type = sniff(b'%PDF-1.4')\nprint(mime_type)  # application/pdf\n\n# Example from file-like object\nwith open('example.html', 'rb') as f:\n    mime_type = sniff(f)\n    print(mime_type)  # text/html","lang":"python","description":"Call sniff() with bytes or a file-like object (opened in binary mode) to get the sniffed MIME type as a string."},"warnings":[{"fix":"Open files in binary mode (e.g., open('file', 'rb')) and ensure input is bytes.","message":"sniff() expects bytes or a binary file-like object. Passing a string or text mode file will raise TypeError or produce incorrect results.","severity":"gotcha","affected_versions":"all"},{"fix":"Use sniff() function for stability.","message":"MimeSniffer class is partially implemented; sniff() function is the recommended entry point. The class API may change in future versions.","severity":"gotcha","affected_versions":"all"},{"fix":"Read the file bytes manually before passing to sniff().","message":"The 'PathLike' support was removed in version 1.0.1 for Python 3.4 compatibility. Using pathlib.Path objects may fail.","severity":"deprecated","affected_versions":"<1.0.1 (does not exist in 1.0.1+)"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Encode the string to bytes: sniff(my_string.encode('utf-8'))","cause":"Passing a string (text) to sniff() instead of bytes.","error":"TypeError: a bytes-like object is required, not 'str'"},{"fix":"Use 'from mimesniff import sniff'.","cause":"Trying to import from a submodule that doesn't exist (e.g., from mimesniff.sniffer import sniff).","error":"ImportError: cannot import name 'sniffer' from 'mimesniff'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}