Skip to main content

ABNF

PyPI abnf-tox CodeQL Docs

ABNF generates parsers from ABNF grammars as described in RFC 5234 and RFC 7405. Its main purpose is parsing data specified in RFCs — HTTP headers, email addresses, URIs, and the like — but it handles any ABNF grammar. It ships with 30+ ready-to-use grammar modules for common RFCs and has been in production use since it was first written for parsing HTTP headers in a web framework.

Installation

pip install abnf
pip install 'abnf[rust]'   # optional Rust backend, for substantially faster parsing

ABNF is tested with Python 3.10–3.14.

Quick start

from abnf.grammars import rfc7232

# parse an ETag header value
node, offset = rfc7232.Rule("ETag").parse('W/"moof"', 0)
print(node.value)          # 'W/"moof"'

# validate a whole string against a rule (raises ParseError otherwise)
from abnf.grammars import rfc5322
rfc5322.Rule("address").parse_all("test@example.com")

# compile your own rule; the RFC 5234 core rules are always available
from abnf import Rule
greeting = Rule.create('greeting = "hello" SP 1*ALPHA')
greeting.parse_all("hello world")

Parsing bytes

abnf parses code points, so parse and parse_all take a str. To parse wire data, decode it with latin-1 — that maps the 256 byte values onto U+0000U+00FF one to one, so a code point is exactly an octet and octet-oriented grammars behave as their RFCs describe:

rfc7230.Rule("request-line").parse_all(raw.decode("latin-1"))

See What abnf parses for why the encoding is the caller's choice.

Documentation

Full documentation is hosted at abnf.readthedocs.io and follows the Diátaxis framework:

  • TutorialParse your first header, a ten-minute end-to-end walkthrough.
  • How-to guides — validate input, extract values with visitors, load a grammar from a file, write your own grammar module, exclude matches from a rule, use the Rust backend.
  • Reference — the public API, the built-in core rules, the bundled grammars, and configuration knobs.
  • Explanation — what abnf parses (code points, not bytes), the combinator architecture, the two backends, alternation semantics, and how backtracking is kept in check.

To build the docs locally:

pip install -e '.[docs]'
sphinx-build -W docs docs/_build/html

Contributing

Set up a development environment with the dev extra:

pip install -e '.[dev]'          # or: uv sync --extra dev

Run the test suite (skip the slower fuzz tests with --ignore=tests/fuzz):

pytest --cov-report term-missing --cov=abnf

Pre-commit hooks run ruff, pyright, check-manifest, and tox. Install them once with pre-commit install. See the Explanation and How-to docs for working with the Rust backend.

Third-party packages

Download files

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

Source Distribution

abnf-2.8.2.tar.gz (961.1 kB view details)

Uploaded Source

Built Distribution

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

abnf-2.8.2-py3-none-any.whl (69.8 kB view details)

Uploaded Python 3

File details

Details for the file abnf-2.8.2.tar.gz.

File metadata

  • Download URL: abnf-2.8.2.tar.gz
  • Upload date:
  • Size: 961.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for abnf-2.8.2.tar.gz
Algorithm Hash digest
SHA256 6f20bf1246d0cd21da51c8f65a37893ad12a12daf0db6adf941d16f443a13866
MD5 f59b68bb8e20c3a93388cdf5c5967a6e
BLAKE2b-256 8eb47ac293941ff2dac90fe667d2ecdda5763cc10c2644b4ae216741f53f4810

See more details on using hashes here.

Provenance

The following attestation bundles were made for abnf-2.8.2.tar.gz:

Publisher: release.yml on declaresub/abnf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file abnf-2.8.2-py3-none-any.whl.

File metadata

  • Download URL: abnf-2.8.2-py3-none-any.whl
  • Upload date:
  • Size: 69.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for abnf-2.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0549f84ad1f2bd29596e10297b8810d4093a6dcab1403a3cf21e0002afa21608
MD5 1632e961d5cfe13d261fa240f2df2202
BLAKE2b-256 9b95bd1b0752644d5619e946447ad21a03e62657de0c4f6eaa1230cbac5bfc41

See more details on using hashes here.

Provenance

The following attestation bundles were made for abnf-2.8.2-py3-none-any.whl:

Publisher: release.yml on declaresub/abnf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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