Parsers for ABNF grammars.
Actively maintained. Last release 6 weeks ago, commits in 21 of the last 26 weeks. A safe choice.
pip install abnfABNF
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.