跳转到主要内容

模拟较差的网络连接。

项目描述

PyPI PyPI - Python Versions PyPI - Implementation Documentation PyPI - License Pipeline Status Coverage

Poorconn是一个Python包,用于模拟较差的网络条件。它适用于测试目的,适用于Python和非Python项目。

它可以模拟以下较差的网络条件

  • 受限制的网络连接。(delay_before_sendingdelay_before_sending_upon_acceptance

  • 在接收连接后立即断开连接的服务器。(close_upon_acceptance

  • 开始时速度较慢,但随后变为正常的连接。(delay_before_sending_oncedelay_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_acceptancehttpd.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

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

poorconn-0.2.1.tar.gz (47.4 kB 查看哈希值)

上传时间

构建分发

poorconn-0.2.1-py3-none-any.whl (48.0 kB 查看哈希值)

上传时间 Python 3

由以下组织支持