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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59f370905262c5d5f2a189713c080e8bd12021c000aa9381f72a7fcc50265879
|
|
| MD5 |
a6741a0819781f969c01b63dd0ba3e18
|
|
| BLAKE2b-256 |
eff447cf76fa8f5c7442631df926e025efebd8096dd6b49df4dffa5ac88f312e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be894c8a5f8a6d0618053792f2940473a4eb8236ae945ce2569d798b9dae5e51
|
|
| MD5 |
e40f32955fa9f7ce0ba9cbb1b2d59333
|
|
| BLAKE2b-256 |
f6c5eeb1cb965e90fc51dc627b512a8cecb4c36ea4e22e6abdd4c4bc700f4d14
|