Parse and serialise HTTP Structured Field Values
pip install http-sfvHTTP Structured Field Values in Python
This Package is DEPRECATED
See the http-sf package for a replacement. This package will only be updated for security issues, and may be yanked in the future.
Introduction
This is a Python 3 library implementing parsing and serialisation of HTTP Structured Fields.
The library's initial purpose is to prove the algorithms in the specification; as a result, it is not at all optimised. It tracks the specification closely, but since it is not yet an RFC, may change at any time.
Currently, this implements draft-ietf-httpbis-sfbis-03.
Python API
There are three top-level types for Structured Field Values; Dictionary, List and Item. After instantiation, each can be used to parse a string HTTP header field value by calling .parse():
>>> from http_sfv import List
>>> my_list = List()
>>> my_list.parse(b"foo; a=1, bar; b=2")
Note that .parse() takes a bytes-like object. If you want to parse a string, please .encode() it first.
Members of Lists and Dictionaries are available by normal Pythonic list and dictionary methods, respectively:
>>> my_list
[<http_sfv.item.Item object at 0x106d25190>, <http_sfv.item.Item object at 0x106d25210>]
>>> my_list[0]
<http_sfv.item.Item object at 0x106d25190>
Items (whether top-level or inside a list or dictionary value) can have their values accessed with the .value property:
>>> my_list[0].value
'foo'
Parameters on Items (and Inner Lists) can be accessed using the .params property, which is a dictionary:
>>> my_list[0].params['a']
1
Note that Tokens and Strings both evaluate as Python strings, but Tokens have a different class:
>>> type(my_list[0].value)
<class 'http_sfv.token.Token'>