URITools
URITools is a Python library providing comprehensive URI parsing, classification, and composition capabilities. It aims to offer a more consistent and feature-rich alternative to the standard library's `urllib.parse` for handling URIs, ensuring robust and compliant URI manipulation. The current version is 6.0.1, and it maintains a regular, though not rapid, release cadence, with major versions typically every 1-2 years.
Warnings
- breaking Starting with version 6.0.0, `uritools` requires Python 3.10 or newer. Installing on older Python versions will fail.
- breaking The functions `splituri` and `composeuri` were removed in version 5.0.0. They were deprecated in version 4.x in favor of `urisplit` and `uricompose`.
- gotcha `uritools` is an alternative to `urllib.parse`, not a drop-in replacement. While similar in purpose, function names, object structures (e.g., `urisplit` returns a `URIComponents` object, not a tuple), and parsing logic can differ significantly. Direct migration without checking the documentation will likely lead to errors.
- gotcha The `port` attribute of the `URIComponents` object (returned by `urisplit`) will be `None` if the port is not explicitly specified in the URI, even if a default port for the scheme exists (e.g., 80 for HTTP, 443 for HTTPS). It will only reflect an explicit port number.
Install
-
pip install uritools
Imports
- urisplit
from uritools import urisplit
- uricompose
from uritools import uricompose
- urirecompose
from uritools import urirecompose
- uricomponent
from uritools import uricomponent
Quickstart
from uritools import urisplit, uricompose
# Parse a URI into its components
uri_string = 'https://user:pass@www.example.com:8080/foo/bar?q=baz&x=y#frag'
p = urisplit(uri_string)
print(f"Scheme: {p.scheme}")
print(f"Userinfo: {p.userinfo}")
print(f"Host: {p.host}")
print(f"Port: {p.port}")
print(f"Path: {p.path}")
print(f"Query: {p.query}")
print(f"Fragment: {p.fragment}")
# Recompose the URI from its components
recomposed_uri = uricompose(p)
print(f"Recomposed URI: {recomposed_uri}")
# Example of getting query parameters
print(f"Query parameters: {p.get_query_dict()}")