Skip to main content

Poorconn: Simulating Poor Network Conditions

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

Poorconn is a Python package that simulates poor network conditions. It is suitable for testing purposes, for both Python and non-Python projects.

It is capable of simulating the following poor network conditions:

  • Throttled network connections. (delay_before_sending, delay_before_sending_upon_acceptance)

  • Servers that cut off connections immediately upon accepting them. (close_upon_acceptance)

  • Connections that are initially slow, but become normal subsequently. (delay_before_sending_once, delay_before_sending_upon_acceptance_once)

Quickstart

Install this package via

$ pip install 'poorconn[full]'  # or "pip install poorconn" if you don't need pytest support

Command Line Usage

The following example starts a local HTTP server at port 8000 that hosts static files at the current working directory. It always closes connections upon accepting them:

$ python -m poorconn -H localhost -p 8000 close_upon_acceptance

In this command, python -m poorconn invokes Poorconn’s command line entrypoint, -H localhost specifies the hostname, -p 8000 specifies the port number, and close_upon_acceptance is a simulation command that simulates a specific poor network conditions, which in this case is closing connections upon accepting them.

After running the command above, connections from a client would establish but fail to communicate subsequently:

$ 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.

For another example, to start a local HTTP server that always throttle connections upon accepting them, simply replace close_upon_acceptance above with delay_before_sending_upon_acceptance --t=1 --length=1024:

$ python -m poorconn delay_before_sending_upon_acceptance --t=1 --length=1024

Here, -H localhost -p 8000 is omitted because it’s Poorconn’s default host and port settings. In this command, poorconn.delay_before_sending_upon_acceptance delays roughly 1 seconds for every 1024 bytes sent. The connection is now throttled:

$ 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]

(Output above is abridged.)

Run python -m poorconn -h to view the help message and see the poorconn module API references for a list of simulation functions (which share the same names with simulation commands).

Docker Image

You can also run Poorconn as a command line tool using our Docker image by replacing python -m poorconn with docker run registry.gitlab.com/xuhdev/poorconn. For instance, the following achieves the same effect as the second command line example above:

$ docker run -p 8000:8000 registry.gitlab.com/xuhdev/poorconn delay_before_sending_upon_acceptance --t=1 --length=1024

Usage in Python

Running the following Python script achieves the same effects as the first command line example above:

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()

The code snippet above is very similar to that runs a basic http server in Python, except that the socket object httpd.socket is patched by poorconn.close_upon_acceptance before http server is running.

For the second command line example above, simply replace close_upon_acceptance(s) above with delay_before_sending_upon_acceptance(s, t=1, length=1024) and adjust imports.

Integration with Pytest

If you use pytest, you can also take advantage of poorconn fixtures in poorconn.pytest_plugin. The following example gets you started with testing against a slow HTTP server:

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

Bug Reports and Feature Requests

Please open a ticket on the GitLab Issue Tracker.

Contributing

Contributions are welcome! To get started, check out CONTRIBUTING.

Metadata

Release files for poorconn 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for poorconn 0.2.1
File Size Uploaded Provenance
poorconn-0.2.1.tar.gz 47.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for poorconn 0.2.1
File Interpreter ABI Platform
poorconn-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 95.3 kB

Release files / poorconn-0.2.1.tar.gz

Download URL poorconn-0.2.1.tar.gz
Size 47.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e97f63d1a807a44a062cfd3d6a14055c330d4461967038204781d39f18908d83
BLAKE2b-256 checksum
How to use checksums
21295c5de25c7b30d19a56a3c3c5105bee6e90bded9965838d4885eadf6390b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2

Release files / poorconn-0.2.1-py3-none-any.whl

Download URL poorconn-0.2.1-py3-none-any.whl
Size 48.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e225f61921f841f7db64689a4684a68f6e9f061d01bc43fb22ccdb61cd76013
BLAKE2b-256 checksum
How to use checksums
021f169a17137287d6c06a05754634bbb3c767df25d5d9550b99b11b743f8e1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2

Security for poorconn 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page