根据RFC 5988“Web链接”解析和格式化链接头
LinkHeader的Python项目详细描述
根据RFC 5988“Web链接”解析和格式化链接头。
用法(假设环境中有合适的headers对象):
>>> headers['Link'] = str(LinkHeader([Link("http://example.com/foo", rel="self"), ... Link("http://example.com", rel="up")])) >>> headers['Link'] '<http://example.com/foo>; rel=self, <http://example.com>; rel=up' >>> parse(headers['Link']) LinkHeader([Link('http://example.com/foo', rel='self'), Link('http://example.com', rel='up')])
空白值和缺失值往返正确:
>>> format_link(parse('</s/1>; obs; if="core.s"; foo=""')) '<</s/1>; obs; if=core.s; foo="">'
还提供了与json友好的基于列表的结构之间的转换:
>>> parse(headers['Link']).to_py() [['http://example.com/foo', [['rel', 'self']]], ['http://example.com', [['rel', 'up']]]] >>> str(LinkHeader([['http://example.com/foo', [['rel', 'self']]], ... ['http://example.com', [['rel', 'up']]]])) '<http://example.com/foo>; rel=self, <http://example.com>; rel=up'