模拟较差的网络连接。
项目描述
Poorconn是一个Python包,用于模拟较差的网络条件。它适用于测试目的,适用于Python和非Python项目。
它可以模拟以下较差的网络条件
受限制的网络连接。(delay_before_sending,delay_before_sending_upon_acceptance)
在接收连接后立即断开连接的服务器。(close_upon_acceptance)
开始时速度较慢,但随后变为正常的连接。(delay_before_sending_once,delay_before_sending_upon_acceptance_once)
快速入门
通过以下方式安装此软件包
$ pip install 'poorconn[full]' # or "pip install poorconn" if you don't need pytest support
命令行使用
以下示例在端口8000上启动一个本地HTTP服务器,该服务器在当前工作目录中托管静态文件。它总是在接收连接后关闭连接
$ python -m poorconn -H localhost -p 8000 close_upon_acceptance
在此命令中,python -m poorconn 调用 Poorconn 的命令行入口点,-H localhost 指定主机名,-p 8000 指定端口号,而 close_upon_acceptance 是一个 模拟命令,模拟特定的网络状况,在此情况下是接受连接后关闭连接。
运行上述命令后,客户端的连接会建立但随后无法通信。
$ wget -t 1 http://127.0.0.1:8000
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... No data received.
Giving up.
例如,要启动一个本地 HTTP 服务器,该服务器在接受连接时总是限制连接,只需将上面的 close_upon_acceptance 替换为 delay_before_sending_upon_acceptance --t=1 --length=1024
$ python -m poorconn delay_before_sending_upon_acceptance --t=1 --length=1024
这里,-H localhost -p 8000 被省略,因为它是 Poorconn 的默认主机和端口设置。在此命令中,poorconn.delay_before_sending_upon_acceptance 在发送每 1024 字节时延迟大约 1 秒。现在连接被限制。
$ wget http://127.0.0.1:8000
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1609 (1.6K) [text/html]
Saving to: 'index.html'
index.html 1.57K 804 B/s in 2.0s <====== NOTE the time
'index.html' saved [1609/1609]
(上述输出已省略。)
运行 python -m poorconn -h 查看帮助信息,并查看 poorconn 模块 API 参考 以获取模拟函数的列表(这些函数与模拟命令具有相同的名称)。
Docker 镜像
您还可以使用我们的 Docker 镜像 将 Poorconn 作为命令行工具运行,方法是使用 docker run registry.gitlab.com/xuhdev/poorconn 替换 python -m poorconn。例如,以下命令与上面的第二个命令行示例具有相同的效果。
$ docker run -p 8000:8000 registry.gitlab.com/xuhdev/poorconn delay_before_sending_upon_acceptance --t=1 --length=1024
Python 中的使用
运行以下 Python 脚本可以达到上面第一个命令行示例的效果。
from http.server import HTTPServer, SimpleHTTPRequestHandler
from poorconn import close_upon_acceptance, make_socket_patchable
# Start a local HTTP server that always closes connections upon established
with HTTPServer(("localhost", 8000), SimpleHTTPRequestHandler) as httpd:
httpd.socket = make_socket_patchable(httpd.socket)
close_upon_acceptance(httpd.socket)
httpd.serve_forever()
上述代码片段与在 Python 中运行基本 HTTP 服务器的代码非常相似,只是在 HTTP 服务器运行之前,通过 poorconn.close_upon_acceptance 对 httpd.socket 对象进行了修复。
对于上面的第二个命令行示例,只需将 close_upon_acceptance(s) 替换为 delay_before_sending_upon_acceptance(s, t=1, length=1024) 并调整导入即可。
与 Pytest 集成
如果您使用 pytest,您还可以利用 poorconn.pytest_plugin 中的 poorconn 固定装置。以下示例可以开始测试对慢速 HTTP 服务器的测试。
pytest_plugins = ('poorconn',)
from pathlib import Path
import time
import requests
import pytest
@pytest.mark.poorconn_http_server_config(t=2, length=1024)
def test_slow_http_server(poorconn_http_server, tmp_path):
"Test GETing from a slow local http server that delays 2 seconds to send every 1024 bytes."
(tmp_path / 'index.txt').write_bytes(b'h' * 1024)
starting_time = time.time()
# Replace the following line with the program you want to test
content = requests.get(f'{poorconn_http_server.url}/index.txt').content
ending_time = time.time()
assert ending_time - starting_time > 2
错误报告和功能请求
请在 GitLab 问题跟踪器 上提交工单。
贡献
欢迎贡献!要开始,请查看 CONTRIBUTING。
版权和许可
除非某些文件的标题中另有说明,否则本项目中所有文件均根据 LGPLv3+ 许可。
版权 (C) 2020–2021 Hong Xu <hong@topbug.net>
本程序是免费软件:您可以重新分配它并/或根据自由软件基金会发布的 GNU 较小通用公共许可证的条款修改它,许可证版本为 3,或者(根据您的选择)任何较新版本。
本程序是根据希望它会有所帮助而分发的,但没有任何保证;甚至没有关于适销性或针对特定目的的适用性的暗示保证。有关详细信息,请参阅 GNU 较小通用公共许可证。
您应该已经收到与此程序一起的GNU通用公共许可证副本。如果没有,请参阅https://gnu.ac.cn/licenses/。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
构建分发
poorconn-0.2.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e97f63d1a807a44a062cfd3d6a14055c330d4461967038204781d39f18908d83 |
|
MD5 | bfc90371cbb7da6eee4a81d0da118827 |
|
BLAKE2b-256 | 21295c5de25c7b30d19a56a3c3c5105bee6e90bded9965838d4885eadf6390b0 |
poorconn-0.2.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8e225f61921f841f7db64689a4684a68f6e9f061d01bc43fb22ccdb61cd76013 |
|
MD5 | e620c9aa82798da075fdac94d9c63bfc |
|
BLAKE2b-256 | 021f169a17137287d6c06a05754634bbb3c767df25d5d9550b99b11b743f8e1e |