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, 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.7.0.tar.gz (928.2 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.7.0-py3-none-any.whl (63.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for abnf-2.7.0.tar.gz
Algorithm Hash digest
SHA256 ad44510424c4f709056567b94d7ba0675b1d6d42676750152556034c98b124cf
MD5 3a257ca3a76eec32c3b250ce91a8261a
BLAKE2b-256 cfad3121dbe0654039513405dec6aad29dc90e470c30b6f66c363d732551475d

See more details on using hashes here.

Provenance

The following attestation bundles were made for abnf-2.7.0.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.7.0-py3-none-any.whl.

File metadata

  • Download URL: abnf-2.7.0-py3-none-any.whl
  • Upload date:
  • Size: 63.5 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.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 991a0ae194084de869305713a581d64e1b47ae5eb9d5735c45947e735cdd9edb
MD5 e18b8c450a9cda88a7e89baaf2bc64fc
BLAKE2b-256 1edafc0202e909f47ca41c11ca5c4fe0b8bb772c858b0b116b2fde1ec64e090c

See more details on using hashes here.

Provenance

The following attestation bundles were made for abnf-2.7.0-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