Registry / http-networking / requests-unixsocket2

requests-unixsocket2

JSON →
library1.0.1pypypi✓ verified 24d ago

requests-unixsocket2 allows the popular 'requests' HTTP library to communicate over UNIX domain sockets. It is a maintained fork of the original 'requests-unixsocket' project, created to ensure compatibility with recent versions of 'requests' and 'urllib3'. The current version is 1.0.1, with releases typically occurring as needed to address compatibility issues or bug fixes.

pip install requests-unixsocket2
INSTALL
IMPORT
SIG · REQUESTS-UNIXSOCKE
R
requests-unixsocket2
http-networkingpythonv1.0.1
Install
2.1s avg
Import
342ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.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.103.95 runs
installs and imports cleanly · install 0.0s · import 0.352s · 21.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.1s · import 0.332s · 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_unixsocket.Session
Commonly imported directly as 'Session' for explicit usage.
monkeypatch
from requests_unixsocket import monkeypatch
requests.monkeypatch()
The monkeypatch function is directly under the 'requests_unixsocket' module.

This quickstart demonstrates how to make an HTTP GET request to a service listening on a UNIX domain socket using an explicit `Session` object. The socket path must be URL-encoded. Error handling is included for common connection and JSON decoding issues.

import requests_unixsocket import json import os # Example: Connect to a hypothetical service via a UNIX domain socket. # Replace '/tmp/my_service.sock' with your actual socket path and '/info' with your API endpoint. # The socket path must be URL-encoded, e.g., '/' becomes '%2F'. # For demonstration purposes, use an environment variable or a common placeholder. # In a real application, ensure the service is running and listening on this socket. unix_socket_path = os.environ.get('UNIX_SOCKET_PATH', '/tmp/my_service.sock') encoded_unix_socket_path = unix_socket_path.replace("/", "%2F") session = requests_unixsocket.Session() try: # Construct the URL using the 'http+unix://' scheme response = session.get(f'http+unix://{encoded_unix_socket_path}/info') response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx) print("Response from UNIX socket service:") print(json.dumps(response.json(), indent=2)) except requests_unixsocket.exceptions.ConnectionError as e: print(f"Could not connect to UNIX socket at {unix_socket_path}: {e}") print("Please ensure a service is running and listening on this socket.") except json.JSONDecodeError: print(f"Received non-JSON response or empty response from {unix_socket_path}. Status: {response.status_code}") print(f"Response text: {response.text}") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
breakingThe original `requests-unixsocket` library is considered abandoned and is incompatible with recent versions of `requests` and `urllib3`. `requests-unixsocket2` was created to address these compatibility issues and is the recommended alternative.
fix
Migrate to `requests-unixsocket2` by installing it and updating import statements (if not already using `from requests_unixsocket import ...`).
affects: <1.0.0 (for requests-unixsocket)
gotchaDirect calls to `requests.get()`, `requests.post()`, etc., will not utilize UNIX domain sockets by default. You must either instantiate `requests_unixsocket.Session()` and use its methods, or call `requests_unixsocket.monkeypatch()` to globally enable UNIX socket support for `requests` functions.
fix
Use `session = requests_unixsocket.Session()` and then `session.get(...)` or call `requests_unixsocket.monkeypatch()` at the start of your application.
affects: All versions of requests-unixsocket2
gotchaAbstract namespace sockets (e.g., URLs like `http+unix://\0test_socket/path`) are a Linux-specific feature. Code using abstract namespace sockets will not work on other operating systems.
fix
Ensure your deployment environment is Linux if using abstract namespace sockets, or use file-based UNIX domain sockets for broader compatibility.
affects: All versions
Errors
Common errors & fixes
requests.exceptions.MissingSchema: Invalid URL "unix+http://...": No schema supplied. Perhaps you meant http://unix+http://...?
The 'unix+http://' or 'unix://' URL schema is not recognized by the 'requests' library because 'requests-unixsocket2.monkeypatch()' was not called.
fix
Call `requests_unixsocket2.monkeypatch()` at the start of your application to register the UNIX domain socket adapter with the 'requests' library.
```python
import requests_unixsocket2
import requests

requests_unixsocket2.monkeypatch()

s = requests.Session()
r = s.get('unix+http://localhost/path/to/socket.sock/api/endpoint')
print(r.text)
```
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
The UNIX domain socket file specified in the URL does not exist, is misspelled, or the path is incorrect.
fix
Verify that the UNIX domain socket path in your URL is absolutely correct and that the socket file actually exists at that location on your filesystem.
```python
import requests_unixsocket2
import requests
import os

requests_unixsocket2.monkeypatch()

socket_path = '/var/run/docker.sock' # Example: Ensure this path is correct
if not os.path.exists(socket_path):
    print(f"Error: Socket file '{socket_path}' does not exist. Check path or ensure service is running.")
else:
    s = requests.Session()
    try:
        r = s.get(f'unix+http://localhost{socket_path}/v1.24/containers/json')
        r.raise_for_status()
        print(r.json())
    except requests.exceptions.ConnectionError as e:
        print(f"Connection error: {e}")
```
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
The UNIX domain socket exists, but the service expected to be listening on it is either not running, has crashed, or does not accept connections from the current user due to permissions.
fix
Ensure the service listening on the UNIX domain socket is running and healthy, and that the user running your script has sufficient permissions to access and connect to the socket file.
```python
import requests_unixsocket2
import requests

requests_unixsocket2.monkeypatch()

s = requests.Session()
try:
    # Example: Replace with your actual socket path and endpoint
    r = s.get('unix+http://localhost/var/run/your_service.sock/status')
    r.raise_for_status()
    print(r.text)
except requests.exceptions.ConnectionError as e:
    print(f"Connection refused. Ensure the service listening on the socket is running and check permissions: {e}")
except requests.exceptions.RequestException as e:
    print(f"Request failed: {e}")
```
ModuleNotFoundError: No module named 'requests_unixsocket'
You have installed the `requests-unixsocket2` library, but your code is attempting to import the original, older library named `requests_unixsocket` which is not installed.
fix
Update your import statements to use `requests_unixsocket2` instead of `requests_unixsocket`.
```python
# Incorrect import:
# import requests_unixsocket
# requests_unixsocket.monkeypatch()

# Correct import:
import requests_unixsocket2
requests_unixsocket2.monkeypatch()

# Your requests code continues as usual
s = requests.Session()
r = s.get('unix+http://localhost/path/to/socket.sock/api')
```
Upgrade
Version history
1.0.1latest on PyPI · released Sep 3, 2025
Audit
Dependencies
requestsrequiredCore functionality relies on and extends the 'requests' library.
Agent activity
9 hits · last 30 days
node
8
Resources
requests-unixsocket2 — pip install requests-unixsocket2 · libregistry