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.1.tar.gz (958.3 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.1-py3-none-any.whl (69.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: abnf-2.8.1.tar.gz
  • Upload date:
  • Size: 958.3 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.1.tar.gz
Algorithm Hash digest
SHA256 ec735ac1630f2cf7f45277a947491bfb5fc23b9214a268c4c3cf409a4a59e108
MD5 3853e35da64f176786f3d9f9eaf59e81
BLAKE2b-256 65a236b74a14362fb667f903c08ab7e4b63075c59dc68a9648e1967aac2e9a56

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: abnf-2.8.1-py3-none-any.whl
  • Upload date:
  • Size: 69.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c6a9ec79f1ba0d29e19e33fc176434def428402df73e1d11bd330a2a8e12273
MD5 233178609443cf3d08e41fac3e260d48
BLAKE2b-256 bb6d05ff50a27874613436c2a14254d2601b512575b13d22e59ddf48cd333593

See more details on using hashes here.

Provenance

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