Python MIME Magic
Filed under Python on apr 02, 2010
The default Python mimetypes implementation is very basic, and limited to guessing mimetypes based on the file extension. This renders the module pretty useless to determine filetypes for uploads and files from other untrusted sources.
>>> import mimetypes >>> mimetypes.guess_type(‘/etc/services’) (None, None) >>> mimetypes.guess_type(‘/usr/bin/who’) (None, None) >>> mimetypes.guess_type(‘/usr/share/xml/schema/xml-core/catalog.xml’) (‘application/xml’, None)
Luckily Adam Hupp wrote a better module, called python-magic which uses a MIME-Magic file to determine file types.
>>> import magic >>> magic.from_file(‘/usr/bin/who’) ‘ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), …’ >>> magic.from_file(‘/usr/bin/who’, mime=True) ‘application/x-executable’ >>> magic.from_file(‘/etc/services’, mime=True) ‘text/plain’ >>> magic.from_file(‘/usr/share/xml/schema/xml-core/catalog.xml’, mime=True) ‘application/xml’
You can find the repository on GitHub (including my win32 patches).
Post your feedback
You can use this form to leave your feedback. Your insights are always appreciated.