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-unixsocket2Verified import paths — ran on the pinned version, not inferred.
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.
Migrate to `requests-unixsocket2` by installing it and updating import statements (if not already using `from requests_unixsocket import ...`).
Use `session = requests_unixsocket.Session()` and then `session.get(...)` or call `requests_unixsocket.monkeypatch()` at the start of your application.
Ensure your deployment environment is Linux if using abstract namespace sockets, or use file-based UNIX domain sockets for broader compatibility.
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)
```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}")
```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}")
```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')
```