Skip to main content

cancelreq

A blocking HTTP/1.1 client whose in-flight requests can be cancelled. Plain HTTP only, no TLS.

Quickstart

import threading

from cancelreq import get

cancel = threading.Event()
body = get(cancel, "http://example.com/")

Set the event from any thread to abort a request that is already running:

import threading

from cancelreq import RequestCancelled, get

cancel = threading.Event()
threading.Timer(0.5, cancel.set).start()

try:
    body = get(cancel, "http://example.com/slow")
except RequestCancelled:
    print("gave up")

POST a body, with headers and a timeout:

from cancelreq import post

body = post(
    cancel,
    "http://example.com/api",
    body='{"answer": 42}',
    headers={"Content-Type": "application/json"},
    timeout=5.0,
)

API

request(cancel, method, url, *, body=None, headers=None, timeout=None) -> bytes
get(cancel, url, *, headers=None, timeout=None) -> bytes
post(cancel, url, *, body=None, headers=None, timeout=None) -> bytes

get and post are shortcuts for request with that method.

Returns the response body. Raises RequestCancelled, HTTPStatusError (4xx/5xx, carrying .status, .reason and .body), TimeoutError, ConnectionError, or ProtocolError.

timeout defaults to None, meaning the request ends only when the server or cancel ends it.

Cancellation

Best effort, and honoured within ~50ms while connecting, while writing the request, and while waiting for the response. A host lookup already in flight runs to completion first.

Development

make test    # pytest
make check   # ty and ruff

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cancelreq-0.1.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cancelreq-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file cancelreq-0.1.0.tar.gz.

File metadata

  • Download URL: cancelreq-0.1.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cancelreq-0.1.0.tar.gz
Algorithm Hash digest
SHA256 59f370905262c5d5f2a189713c080e8bd12021c000aa9381f72a7fcc50265879
MD5 a6741a0819781f969c01b63dd0ba3e18
BLAKE2b-256 eff447cf76fa8f5c7442631df926e025efebd8096dd6b49df4dffa5ac88f312e

See more details on using hashes here.

File details

Details for the file cancelreq-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cancelreq-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for cancelreq-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be894c8a5f8a6d0618053792f2940473a4eb8236ae945ce2569d798b9dae5e51
MD5 e40f32955fa9f7ce0ba9cbb1b2d59333
BLAKE2b-256 f6c5eeb1cb965e90fc51dc627b512a8cecb4c36ea4e22e6abdd4c4bc700f4d14

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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