Python mimetypes - Introduction














































Python mimetypes - Introduction



Introduction


MIME stands for Multipurpose Internet Mail Extension, it is a label used to identify a type of data and serves the same purpose on the Internet that a file extensions does on Operating Systems.

The mimetypes module converts between a filename or URL and the MIME type associated with the  filename extension.

Conversions are provided from filename to MIME type and from MIME type to filename extension; encodings are not supported for the latter conversion.


The module provides one class and a number of convenience functions.

The functions provide the primary interface for this module. If the module has not been initialized,
they will call init() if they rely on the information init() sets up.


This module defines two useful functions:

%     

  • guess_type(url, strict=True) -- guess the MIME type and encoding of a URL.
  • guess_extension(type, strict=True) -- guess the extension for a given MIME type.


It also contains the following, for tuning the behavior:



1.    Data:


1.1. knownfiles -- list of files to parse


1.2. inited -- flag set when init() has been called


1.3. suffix_map -- dictionary mapping suffixes to suffixes


1.4. encodings_map -- dictionary mapping suffixes to encodings


1.5. types_map -- dictionary mapping suffixes to types



2.    Functions:


2.1. init([files]) -- parse a list of files, default knownfiles (on Windows, the default values are taken from the registry)


2.2. read_mime_types(file) -- parse one file, return a dictionary or None



Source code:


 Lib/mimetypes.py



An example usage of the module:


import mimetypes

mimetypes.init()
mimetypes.knownfiles
['/etc/mime.types', '/etc/httpd/mime.types', ... ]

mimetypes.suffix_map['.tgz']

'.tar.gz'

mimetypes.encodings_map['.gz']

'gzip'

mimetypes.types_map['.tgz']

'application/x-tar-gz'


NOTE: Green Box shows the Outputs for the LOC(Line Of Code) above it

NEXT->



More Articles of Shubhanjay Pandey:

Name Views Likes
Python ssl Sockets 998 3
Python unittest.mock side_effect 503 2
Python ssl Example Server Side Operation 771 3
Python unittest.mock method_calls and mock_calls 841 2
Python ssl TLS 1.3 and LibreSSL Support 1450 2
Python unittest.mock The patchers 310 2
Python unittest.mock Attaching Mocks as Attributes 433 2
Python ssl Example Client Side Operations 463 3
Python unittest.mock patch.object and patch.dict 2748 2
Python unittest.mock.call_args and call_args_list 3286 2
Python ssl Certificates 321 3
Python unittest.mock Deleting, Mock Names and the name attribute 517 2
Python unittest.mock FILTER_DIR 254 2
Python ssl Introduction 286 3
Python unitest.mock Where to patch, patching descriptors and proxy objects 268 2
Python ssl Memory BIO Support 539 3
Python mimetypes - Introduction 334 3
Python unittest.mock Helpers call 282 2
Python unittest.mock function assert_*() 283 2
Python unittest.mock Introduction 340 2
Python sunau library 305 2
Python unittest.mock The Mock Class 275 2
Python ssl Constants Part 1 357 3
Python mimetypes.add_type 257 4
Python ssl Socket and Context Creation 1300 3
Python unittest.mock.mock_open 297 2
Python ssl non-blocking sockets 646 3
Python unittest.mock MagicMock and magic method support 847 1
Python unittest.mock Magic Mock 370 2
Python ssl Exceptions 307 3
Python unittest.mock TEST_PREFIX and Nesting Patch Decorators 395 2
Python unittest.mock patch.multiple and builtins 2017 2
Python mimetypes additional data 232 3
Python unittest.mock Other HELPERS 252 2
Python MimeTypes Object 267 3
Python mimetypes.guess_type 332 3
Python unittest.mock Autospeccing 304 2
Python unittest.mock.AsyncMock 2441 2
Python ssl Certificates Handling 375 3
Python ssl Random Generation 290 3
Python unittest.mock Function configure_mock 385 2
Python ssl SSLContexts Part 1 618 3
Python unittest.mock patch methods: start and stop 1693 2
Python unittest.mock Calling 271 2
Python unittest.mock other functions 268 2
Python ssl Constants Part 2 430 3
Python ssl SSLContexts Part 2 764 3
Python ssl Security Considerations and SSL Session 416 3
Python mimetypes.guess_extension 313 3
Python unittest.mock PropertyMock and NonCallableMock 787 2
Python mimetypes additional functions 253 3

Comments