Parsing and validation of URIs (RFC 3986) and IRIs (RFC 3987)

1.3.8 · maintenance · verified Sat Apr 11

The `rfc3987` Python library provides regular expressions and utilities for parsing and validating Uniform Resource Identifiers (URIs) as per RFC 3986 and Internationalized Resource Identifiers (IRIs) according to RFC 3987. The current version is 1.3.8. Development on the original GitHub repository has ceased, and the project is now archived, with development reportedly moved to Codeberg, implying a largely unmaintained or very slow release cadence on PyPI.

Warnings

Install

Imports

Quickstart

Demonstrates parsing an IRI, matching a component against a rule, and resolving a relative URI reference. The `parse` function extracts components into a dictionary, `match` checks if a string conforms to a specific ABNF rule, and `resolve` combines a base URI with a relative reference.

from rfc3987 import parse, match, resolve

# Parse an IRI and get its components
iri_string = 'http://example.com/path?query=value#fragment'
parsed_iri = parse(iri_string, rule='IRI')
print(f"Parsed Scheme: {parsed_iri.get('scheme')}")
print(f"Parsed Authority: {parsed_iri.get('authority')}")
print(f"Parsed Path: {parsed_iri.get('path')}")

# Check if a string matches a specific rule
is_pct_encoded = match('%C7', 'pct_encoded')
print(f"Is '%C7' percent-encoded? {bool(is_pct_encoded)}")

# Resolve a relative URI reference
base_uri = 'http://a/b/c/d;p?q'
relative_ref = '../../g'
resolved_uri = resolve(base_uri, relative_ref)
print(f"Resolved URI: {resolved_uri}")

view raw JSON →