HTTP请求和响应的类型
项目描述
Python中的HTTP类型
Python (3.6或更高版本) 库,用于以HTTP类型格式读取和写入HTTP交换记录。
安装
pip install http-types
编写HTTP交换
使用HttpExchangeWriter
,可以将HTTP流量记录序列化,以便与任何可以处理HTTP类型格式的程序一起使用
request = RequestBuilder.from_dict({
"host": "api.github.com",
"protocol": "https",
"method": "get",
"pathname": "/v1/users",
"query": {"a": "b", "q": ["1", "2"]},
}
)
response = ResponseBuilder.from_dict({
"statusCode": 200,
"headers": {"content-type": "text/plain"},
"body": "(response body string)",
}
)
exchange = HttpExchange(request=request, response=response)
with tempfile.TemporaryFile(mode="w") as output:
writer = HttpExchangeWriter(output)
writer.write(exchange)
# Serialize to dictionary
as_dict = HttpExchangeWriter.to_dict(exchange)
# Serialize to JSON string
as_str = HttpExchangeWriter.to_json(exchange)
读取HTTP交换
使用HttpExchangeReader
,可以读取HTTP类型格式的记录进行处理
for exchange in HttpExchangeReader.from_jsonl(input_file):
assert exchange.request.method == HttpMethod.GET
assert exchange.request.protocol == Protocol.HTTPS
assert exchange.response.statusCode == 200
开发
初始设置
- 创建一个新的虚拟环境。
- 安装依赖项:
pip install --upgrade -e '.[dev]'
为了测试,运行 python setup.py test
,这将
- 使用 black 强制代码格式。
- 使用在 pytest.ini 中配置的
pytest
进行测试。 - 使用
mypy
进行类型检查。 - 使用配置在 .flake8 中的 flake8 强制风格指南。
发布
- 如果版本与已发布的 包 相同,请在 setup.py 中升级版本。提交并推送。
- 运行
python setup.py test
和python setup.py dist
来检查一切是否正常工作。 - 要构建和上传包,请运行
python setup.py upload
。输入 PyPI 凭证以将包上传到PyPI
。该命令还将运行git tag
来标记提交为发布版本,并将标签推送到远程。
要了解不同的命令做什么,请查看 setup.py 中的 Command
类。
项目详情
关闭
http-types-0.0.18.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d3fd2d56784efd0be102d8e47cd8c1fa555ebf03806b940dc1bef20cb4a78a04 |
|
MD5 | ca7a5471f7fd77787bca322c1d3c4df2 |
|
BLAKE2b-256 | 12eed363f0f6597d1eca7da2b96c129ce577984e148377144e06c40fbf754efb |
关闭
http_types-0.0.18-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 274be507e4dea51efe3ad0b40eae730e9b65f07a6b08e6c18191bd894b19ccb7 |
|
MD5 | 1d3870750f6eea3a2c168db472a9bbfc |
|
BLAKE2b-256 | 5cf12e1d75b59c2e9426150fe0054fd908f9933173d4dc9ab6bae50a3448ccf5 |