HTTP消息的代码检查器。
项目描述
httplint
这个Python库可以对HTTP消息进行 代码检查;它会检查其正确性并报告任何找到的问题。
它对REDbot的所有消息级检查都进行了执行,但不通过向网络发送请求进行任何“主动”检查,并且它没有Web用户界面。
将httplint用作库
httplint公开了两个用于代码检查的类:`HttpRequestLinter` 和 `HttpResponseLinter`。它们公开了以下方法来告诉代码检查器有关HTTP消息
- 适当的时候
process_request_topline
,它接受三个bytes
参数:method
、uri
和version
process_response_topline
,它接受三个bytes
参数:version
、status_code
和status_phrase
process_headers
用于头部,接受一个包含 (name
、value
) 元组的列表(都是bytes
)feed_content
用于正文(可以多次调用),接受一个inbytes
参数- 完成时调用
finish_content
,它有两个参数;一个bool
值指示响应是否完整,以及可选的包含拖车信息的元组列表,其格式与process_headers
相同。
例如
from httplint import HttpResponseLinter
linter = HttpResponseLinter()
linter.process_response_topline(b'HTTP/1.1', b'200', b'OK')
linter.process_headers([
(b'Content-Type', b'text/plain'),
(b'Content-Length', b'10'),
(b'Cache-Control', b'max-age=60')
])
linter.feed_content(b'12345')
linter.feed_content(b'67890')
linter.finish_content(True)
从命令行使用httplint
httplint也可以从命令行使用。例如
> curl -s -i --raw https://www.mnot.net/ | httplint -n
* The Content-Length header is correct.
* The resource last changed 8 days 6 hr ago.
* This response allows all caches to store it.
* The server's clock is correct.
* This response is fresh until 3 hr from now.
* This response may still be served by a cache once it becomes stale.
解读说明
一旦消息经过代码检查,结果将显示在 notes
属性中。这是一个 Note
对象的列表,每个对象都具有以下属性
category
- 笔记的分类;请参阅note.categories
level
- 请参阅note.levels
summary
- 笔记的简要一行描述detail
- 更详细的解释
请注意,summary
是文本性的,需要在标记环境中转义;然而,detail
已经是转义后的 HTML。
继续我们的示例
for note in linter.notes:
print(note.summary)
输出应该是
The Content-Length header is correct.
This response allows all caches to store it.
This response doesn't have a Date header.
This response is fresh until 1 min from now.
This response may still be served by a cache once it becomes stale.
字段描述
可以通过调用 get_field_description
来找到任何字段的描述。例如
>>> from httplint import get_field_description
>>> get_field_description("Allow")
'The `Allow` response header advertises the set of methods that are supported by the resource.'
如果找不到描述,它将返回 None
。
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源代码分发
httplint-2024.4.1.tar.gz (70.6 kB 查看哈希值)
构建分发
httplint-2024.4.1-py3-none-any.whl (104.2 kB 查看哈希值)