Skip to main content

Validating URI References per RFC 3986

Project description

A Python implementation of RFC 3986 including validation and authority parsing. Coming soon: Reference Resolution.

Installation

Simply use pip to install rfc3986 like so:

pip install rfc3986

License

Apache License Version 2.0

Example Usage

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')

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.

0.1.0 – 2014-06-27

  • Initial Release includes validation and normalization of URIs

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-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

rfc3986-0.1.0-py2-none-any.whl (13.5 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: rfc3986-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rfc3986-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f16bf0d1b6aa07c20fe15aa8e7ef1f92aaab4833289ab5f894aca583a45691cf
MD5 f9c7f7a9084df9e4beb039a47b9903e2
BLAKE2b-256 44f735fff18772da90916d28a37389040dc111ccfe39f055e75d539876c4bcb9

See more details on using hashes here.

File details

Details for the file rfc3986-0.1.0-py2-none-any.whl.

File metadata

File hashes

Hashes for rfc3986-0.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 20fbe816de910e1090f9c257907c8d2ce4bec554c0a9a734913943cadfe5bc4f
MD5 2e158c72fb421f861f7b85e342913447
BLAKE2b-256 26d622ada9cf61249501623ab0e719b3f79a2dd027404112eb7ce68821c3e09f

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