Registry / http-networking / requests-unixsocket

requests-unixsocket

JSON →
library0.4.1pypypi✓ verified 28d ago

requests-unixsocket is a Python library that allows the popular `requests` HTTP library to communicate over UNIX domain sockets. It extends `requests`' functionality to support `http+unix://` URLs. The current stable version is 0.4.1. Releases appear to be infrequent, focusing on maintenance and compatibility with newer `requests` versions.

pip install requests-unixsocket
INSTALL
IMPORT
SIG · REQUESTS-UNIXSOCKE
R
requests-unixsocket
http-networkingpythonv0.4.1
Install
2.2s avg
Import
350ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.354s · 21.1MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 2.2s · import 0.346s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Session
✓ from requests_unixsocket import Session
✗ import requests; s = requests.Session()
Use the specialized Session for explicit UNIX socket handling.
monkeypatch
✓ import requests_unixsocket; requests_unixsocket.monkeypatch()
Applies global monkeypatching for `requests.get()` to handle `http+unix://` URLs.

This quickstart demonstrates how to use `requests-unixsocket` in two ways: explicitly via `requests_unixsocket.Session` (recommended) and implicitly via `requests_unixsocket.monkeypatch()`. It attempts to connect to a common Docker UNIX domain socket to retrieve system information, handling potential connection errors. The socket path must be URL-percent-encoded.

import requests_unixsocket import os # Example for Docker daemon socket (adjust path if needed) docker_socket_path = '/var/run/docker.sock' # or os.environ.get('DOCKER_SOCKET', '/var/run/docker.sock') # Ensure the socket path exists for a runnable example # In a real scenario, Docker or another service would create this. # For this quickstart, we'll just check if it exists or use a dummy. if not os.path.exists(docker_socket_path): print(f"Warning: Docker socket not found at {docker_socket_path}. Quickstart might not connect.") # Fallback to a non-existent path for structure, but it will fail. docker_socket_path = '/tmp/nonexistent_socket.sock' # Explicit Session usage session = requests_unixsocket.Session() # The socket path must be percent-encoded in the URL host part encoded_socket_path = requests_unixsocket.requests.compat.quote_plus(docker_socket_path) url = f'http+unix://{encoded_socket_path}/info' try: response = session.get(url, timeout=5) response.raise_for_status() # Raise an exception for HTTP errors print("Explicit Session usage successful!") print(f"Status Code: {response.status_code}") print(f"JSON Response Keys: {list(response.json().keys())[:5]}...") except requests_unixsocket.requests.exceptions.ConnectionError as e: print(f"Connection Error: Could not connect to UNIX socket at {docker_socket_path}. Is a service listening there? (Error: {e})") except Exception as e: print(f"An unexpected error occurred: {e}") # --- Alternative: Monkeypatching (affects global requests behavior) --- # This is generally discouraged for libraries, but shown for completeness. # with requests_unixsocket.monkeypatch(): # try: # response_mp = requests_unixsocket.requests.get(url, timeout=5) # response_mp.raise_for_status() # print("\nMonkeypatching usage successful!") # print(f"Status Code: {response_mp.status_code}") # print(f"JSON Response Keys: {list(response_mp.json().keys())[:5]}...") # except requests_unixsocket.requests.exceptions.ConnectionError as e: # print(f"\nMonkeypatching Connection Error: Could not connect to UNIX socket at {docker_socket_path}. (Error: {e})") # except Exception as e: # print(f"\nAn unexpected error occurred with monkeypatching: {e}")
Debug
Known issues
breakingUpdates to the upstream `requests` library can sometimes introduce breaking changes that affect `requests-unixsocket`, particularly related to internal adapter interfaces. While `requests-unixsocket` aims to maintain compatibility, rapid `requests` releases may temporarily break functionality.
fix
Ensure `requests-unixsocket` is updated to the latest version. If issues persist, consider pinning `requests` to a compatible older version or checking the `requests-unixsocket` GitHub issues for workarounds or upcoming fixes. A fork, `requests-unixsocket2`, emerged due to past incompatibilities, indicating this is a recurring concern.
affects: All versions, potentially with newer `requests` versions.
gotchaUNIX socket paths in URLs must be URL-percent-encoded (e.g., `/var/run/docker.sock` becomes `%2Fvar%2Frun%2Fdocker.sock`). Failure to encode correctly will result in incorrect URL parsing and connection errors.
fix
Always use `requests.compat.quote_plus()` or `urllib.parse.quote_plus()` to encode the UNIX socket path before constructing the `http+unix://` URL.
affects: All versions.
gotchaUsing `requests_unixsocket.monkeypatch()` globally alters the default behavior of `requests.get()` and `requests.Session()` (for default sessions). This can lead to unexpected side effects in other parts of an application or library that rely on standard `requests` behavior.
fix
Prefer creating an explicit `requests_unixsocket.Session()` instance for better isolation and control. If monkeypatching is necessary, limit its scope using a `with requests_unixsocket.monkeypatch():` context manager.
affects: All versions.
gotchaThe default timeout for `requests-unixsocket` connections (historically 60 seconds) might differ from the default behavior of `requests` (which has no default timeout). This can lead to unexpected blocking or timeout behavior if not explicitly set.
fix
Always explicitly set the `timeout` parameter in your `session.get()` or `session.post()` calls to ensure consistent and predictable behavior.
affects: Potentially all versions prior to `requests` aligning its own timeout handling with adapters; user-specified timeouts are always respected.
gotchaAbstract namespace sockets (e.g., `http+unix://\0test_socket/`) are a Linux-specific feature and will not work on other operating systems like macOS or Windows.
fix
Ensure that if abstract namespace sockets are used, the application is deployed on a Linux environment. For cross-platform compatibility, prefer file-system based UNIX domain sockets or alternative IPC mechanisms.
affects: All versions.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'requests_unixsocket'
The `requests-unixsocket` library has not been installed in the Python environment, or the environment where the code is being run does not have access to the installed package.
fix
Install the library using pip: `pip install requests-unixsocket`
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
The target UNIX domain socket file specified in the URL does not exist, the server application listening on that socket is not running, or the current user lacks the necessary permissions to access the socket file.
fix
Ensure the server process is running and listening on the specified UNIX domain socket. Verify that the socket file path is correct in the URL and that the Python process has read/write permissions for the socket file and its parent directories.
requests.exceptions.MissingSchema: Invalid URL "http+unix://...": No schema supplied. Perhaps you meant http://http+unix://...?
The `requests-unixsocket` library was imported, but its functionality was not activated either by using a `requests_unixsocket.Session()` object or by globally monkeypatching the `requests` library.
fix
Use `requests_unixsocket.Session()` for explicit usage, or call `requests_unixsocket.monkeypatch()` at the start of your script to enable `http+unix://` URLs with standard `requests` calls.

**Explicit usage:**
```python
import requests_unixsocket
session = requests_unixsocket.Session()
r = session.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
```

**Monkeypatching:**
```python
import requests
import requests_unixsocket
requests_unixsocket.monkeypatch()
r = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
```
requests.exceptions.InvalidSchema: No connection adapters were found for 'http+unix://%2Ftmp%2Ftest.sock/endpoint'
The `requests-unixsocket` adapter was not registered with `requests`, either by calling `requests_unixsocket.monkeypatch()` or manually mounting a `UnixAdapter`.
fix
import requests_unixsocket
requests_unixsocket.monkeypatch()
requests.get('http+unix://%2Ftmp%2Ftest.sock/endpoint')
requests.exceptions.MissingSchema: Invalid URL '/tmp/test.sock': No schema supplied. Perhaps you meant http:////tmp/test.sock?
The URL provided for the UNIX socket connection does not use the required `http+unix://` schema, or the socket path is not properly encoded.
fix
import requests_unixsocket
requests_unixsocket.monkeypatch()
requests.get('http+unix://%2Ftmp%2Ftest.sock/endpoint')
Upgrade
Version history
0.4.1latest on PyPI · released Mar 7, 2025
Audit
Dependencies
requestsrequiredCore library extended by requests-unixsocket for HTTP functionality.
urllib3requiredUnderlying HTTP client library used by requests and implicitly by requests-unixsocket.
Agent activity
6 hits · last 30 days
node
4
Resources
requests-unixsocket — pip install requests-unixsocket · libregistry