Skip to main content

Validating URI References per RFC 3986

Project description

A Python implementation of RFC 3986 including validation and authority parsing.

Installation

Use pip to install rfc3986 like so:

pip install rfc3986

License

Apache License Version 2.0

Example Usage

The following are the two most common use cases envisioned for rfc3986.

Replacing urlparse

To parse a URI and receive something very similar to the standard library’s urllib.parse.urlparse

from rfc3986 import urlparse

ssh = urlparse('ssh://user@git.openstack.org:29418/openstack/glance.git')
print(ssh.scheme)  # => ssh
print(ssh.userinfo)  # => user
print(ssh.params)  # => None
print(ssh.port)  # => 29418

To create a copy of it with new pieces you can use copy_with:

new_ssh = ssh.copy_with(
    scheme='https'
    userinfo='',
    port=443,
    path='/openstack/glance'
)
print(new_ssh.scheme)  # => https
print(new_ssh.userinfo)  # => None
# etc.

Strictly Parsing a URI and Applying Validation

To parse a URI into a convenient named tuple, you can simply:

from rfc3986 import uri_reference

example = uri_reference('http://example.com')
email = uri_reference('mailto:user@domain.com')
ssh = uri_reference('ssh://user@git.openstack.org:29418/openstack/keystone.git')

With a parsed URI you can access data about the components:

print(example.scheme)  # => http
print(email.path)  # => user@domain.com
print(ssh.userinfo)  # => user
print(ssh.host)  # => git.openstack.org
print(ssh.port)  # => 29418

It can also parse URIs with unicode present:

uni = uri_reference(b'http://httpbin.org/get?utf8=\xe2\x98\x83')  # ☃
print(uni.query)  # utf8=%E2%98%83

With a parsed URI you can also validate it:

if ssh.is_valid():
    subprocess.call(['git', 'clone', ssh.unsplit()])

You can also take a parsed URI and normalize it:

mangled = uri_reference('hTTp://exAMPLe.COM')
print(mangled.scheme)  # => hTTp
print(mangled.authority)  # => exAMPLe.COM

normal = mangled.normalize()
print(normal.scheme)  # => http
print(mangled.authority)  # => example.com

But these two URIs are (functionally) equivalent:

if normal == mangled:
    webbrowser.open(normal.unsplit())

Your paths, queries, and fragments are safe with us though:

mangled = uri_reference('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')
normal = mangled.normalize()
assert normal == 'hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth'
assert normal == 'http://example.com/Some/reallY/biZZare/pAth'
assert normal != 'http://example.com/some/really/bizzare/path'

If you do not actually need a real reference object and just want to normalize your URI:

from rfc3986 import normalize_uri

assert (normalize_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth') ==
        'http://example.com/Some/reallY/biZZare/pAth')

You can also very simply validate a URI:

from rfc3986 import is_valid_uri

assert is_valid_uri('hTTp://exAMPLe.COM/Some/reallY/biZZare/pAth')

Requiring Components

You can validate that a particular string is a valid URI and require independent components:

from rfc3986 import is_valid_uri

assert is_valid_uri('http://localhost:8774/v2/resource',
                    require_scheme=True,
                    require_authority=True,
                    require_path=True)

# Assert that a mailto URI is invalid if you require an authority
# component
assert is_valid_uri('mailto:user@example.com', require_authority=True) is False

If you have an instance of a URIReference, you can pass the same arguments to URIReference#is_valid, e.g.,

from rfc3986 import uri_reference

http = uri_reference('http://localhost:8774/v2/resource')
assert uri.is_valid(require_scheme=True,
                    require_authority=True,
                    require_path=True)

# Assert that a mailto URI is invalid if you require an authority
# component
mailto = uri_reference('mailto:user@example.com')
assert uri.is_valid(require_authority=True) is False

Alternatives

  • rfc3987

    This is a direct competitor to this library, with extra features, licensed under the GPL.

  • uritools

    This can parse URIs in the manner of RFC 3986 but provides no validation and only recently added Python 3 support.

  • Standard library’s urlparse/urllib.parse

    The functions in these libraries can only split a URI (valid or not) and provide no validation.

Contributing

This project follows and enforces the Python Software Foundation’s Code of Conduct.

If you would like to contribute but do not have a bug or feature in mind, feel free to email Ian and find out how you can help.

The git repository for this project is maintained at https://github.com/python-hyper/rfc3986

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

rfc3986-2.0.0.tar.gz (49.0 kB view details)

Uploaded Source

Built Distribution

rfc3986-2.0.0-py2.py3-none-any.whl (31.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file rfc3986-2.0.0.tar.gz.

File metadata

  • Download URL: rfc3986-2.0.0.tar.gz
  • Upload date:
  • Size: 49.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for rfc3986-2.0.0.tar.gz
Algorithm Hash digest
SHA256 97aacf9dbd4bfd829baad6e6309fa6573aaf1be3f6fa735c8ab05e46cecb261c
MD5 bbf20302bf26bc771e88cc775fbde3bc
BLAKE2b-256 85401520d68bfa07ab5a6f065a186815fb6610c86fe957bc065754e47f7b0840

See more details on using hashes here.

File details

Details for the file rfc3986-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: rfc3986-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 31.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for rfc3986-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 50b1502b60e289cb37883f3dfd34532b8873c7de9f49bb546641ce9cbd256ebd
MD5 5710e7a2eadc951d2a690a327747f3a2
BLAKE2b-256 ff9a9afaade874b2fa6c752c36f1548f718b5b83af81ed9b76628329dab81c1b

See more details on using hashes here.

Supported by

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