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+0000–U+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:

  • Tutorial — Parse 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

tests/fuzz/ holds two committed corpora, replayed by every run. If you change a grammar module, regenerate them:

python tests/fuzz/gen_corpus.py rfc9051           # Hypothesis, walks the built parser
python tests/fuzz/gen_abnfgen_corpus.py rfc9051   # abnfgen, reads the grammar text

The second needs abnfgen on your PATH (or ABNFGEN set to it); CI only replays what is committed. Reading the text rather than the built parser is the point — it catches a grammar that did not become the parser it describes, which a generator walking that parser cannot.

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

Metadata

Release files for abnf 2.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for abnf 2.9.1
File Size Uploaded Provenance Actions
abnf-2.9.1.tar.gz 1.0 MB With provenance Details for abnf-2.9.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for abnf 2.9.1
File Interpreter ABI Platform Actions
abnf-2.9.1-py3-none-any.whl Python 3 none any Details for abnf-2.9.1-py3-none-any.whl

Total release size: 1.1 MB

Release files / abnf-2.9.1.tar.gz

Download URL abnf-2.9.1.tar.gz
Size 1.0 MB
Tags Source
SHA-256 checksum
How to use checksums
58dfcbec5362fbc25d210ff916c014bc75a5a89e274318c7e9b47c1e8c379c06
BLAKE2b-256 checksum
How to use checksums
002709d5b2e8e0a83a1bb169e4b4382261f4dcf1d0cccaef6306d9962944223a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / abnf-2.9.1-py3-none-any.whl

Download URL abnf-2.9.1-py3-none-any.whl
Size 78.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdcac127b3693ded2b00565878d8ed2b96d34b29f0ee199c06d7b40baaf60917
BLAKE2b-256 checksum
How to use checksums
3f8fbc07d154a9aee9181922d73a5be0c7854bd6e30c932ecc33e95af1ad960e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Security for abnf 2.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for abnf-2.9.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page