Skip to main content

implementation of the data uri scheme defined in rfc2397

Project description

datauri is a small Python library implementing the data: uri scheme defined in RFC2397. The generic format is:

data:[<mediatype>][;base64],<data>

Installation

Install datauri from PyPI using:

pip install datauri

Note: this library is for Python 3 only.

Usage

>>> import datauri

To parse a string containing a data: uri, use parse():

>>> parsed = datauri.parse('data:text/plain,A%20brief%20note')

This returns a parse result:

>>> parsed.media_type
'text/plain'
>>> parsed.data
b'A brief note'
>>> parsed.uri
'data:text/plain,A%20brief%20note'

This is a simple container class with a few attributes:

  • The media_type attribute is a string (str) or None in case it was absent. Any (optional) embedded text encoding specifications are not processed in any way; it is up to the application to deal with that.

  • The data attribute is a byte string (bytes) with the decoded data. URL encoding and base64 is handled transparently.

  • For convenience, the uri attribute contains the input uri.

Parsed URIs compare equal if their media type and data are the same. Instances are hashable, so they can be used as dictionary keys.

Parsing failures will raise a DataURIError:

>>> datauri.parse('invalid')
Traceback (most recent call last):

datauri.DataURIError: invalid data uri

The DataURIError subclasses the built-in ValueError, so this will work as expected:

try:
    datauri.parse('invalid')
except ValueError:
    pass

In addition to parsing a string, this library can also discover (and directly parse) any data: URIs found in a larger string:

s = 'long string with data:text/plain,A%20brief%20note and more'
for parsed in datauri.discover(s):
    print(s)

More information

Known issues

Currently, only parsing has been implemented.

Contributing

Please use Github issues to report problems or propose improvements.

Version history

  • 1.1.0 (2026-02-17)

    Support for current Python versions

  • 1.0.0 (2017-10-05)

    Initial release

License

(This is the OSI approved 3-clause “New BSD License”.)

Copyright © 2017, EclecticIQ

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datauri-1.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datauri-1.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file datauri-1.1.0.tar.gz.

File metadata

  • Download URL: datauri-1.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for datauri-1.1.0.tar.gz
Algorithm Hash digest
SHA256 30f6ac2a3143ce0ee23bd2b956c6bfcc76974782b1016618d1b66a3f3a9594c1
MD5 b68f6f4fa77b315704f864212c48dd00
BLAKE2b-256 6df849394b0885a363662dac56983f4983728da36327f9bc460ae9fc61d8a41e

See more details on using hashes here.

File details

Details for the file datauri-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: datauri-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for datauri-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfb4726a1b29aae7809662e8c1352acb325579c0b25e2d75e656ccdd7f9d7edb
MD5 0c2be44d2886e822f50d344ba1a4ad22
BLAKE2b-256 fe574a391c25aea199104d7d12202113749b4c92fd1ee07b136a24c9594bcc78

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page