# requests-unixsocket2
<a href="https://badge.fury.io/py/requests-unixsocket2">
<img src="https://badge.fury.io/py/requests-unixsocket2.svg" alt="Latest Version on PyPI" />
</a>
<a href="https://gitlab.com/thelabnyc/requests-unixsocket2/-/commits/master">
<img alt="pipeline status" src="https://gitlab.com/thelabnyc/requests-unixsocket2/badges/master/pipeline.svg" />
</a>
<a href="https://gitlab.com/thelabnyc/requests-unixsocket2/-/commits/master">
<img alt="coverage report" src="https://gitlab.com/thelabnyc/requests-unixsocket2/badges/master/coverage.svg" />
</a>
<a href="https://gitlab.com/thelabnyc/requests-unixsocket2/-/releases">
<img alt="Latest Release" src="https://gitlab.com/thelabnyc/requests-unixsocket2/-/badges/release.svg" />
</a>
Use [requests](http://docs.python-requests.org/) to talk HTTP via a UNIX domain socket.
## Usage
### Explicit
You can use it by instantiating a special `Session` object:
```py
import json
import requests_unixsocket
session = requests_unixsocket.Session()
r = session.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
registry_config = r.json()['RegistryConfig']
print(json.dumps(registry_config, indent=4))
```
### Implicit (monkeypatching)
Monkeypatching allows you to use the functionality in this module, while making minimal changes to your code. Note that in the above example we had to instantiate a special `requests_unixsocket.Session` object and call the `get` method on that object. Calling `requests.get(url)` (the easiest way to use requests and probably very common), would not work. But we can make it work by doing monkeypatching.
You can monkeypatch globally:
```py
import requests_unixsocket
requests_unixsocket.monkeypatch()
r = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
assert r.status_code == 200
```
or you can do it temporarily using a context manager:
```py
import requests_unixsocket
with requests_unixsocket.monkeypatch():
r = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
assert r.status_code == 200
```
### Abstract namespace sockets
To connect to an [abstract namespace socket](https://utcc.utoronto.ca/~cks/space/blog/python/AbstractUnixSocketsAndPeercred) (Linux only), prefix the name with a NULL byte (i.e.: `\0`) - e.g.:
```py
import requests_unixsocket
session = requests_unixsocket.Session()
res = session.get('http+unix://\0test_socket/get')
print(res.text)
```
For an example program that illustrates this, see `examples/abstract_namespace.py` in the git repo. Since abstract namespace sockets are specific to Linux, the program will only work on Linux.
## See also
- https://github.com/msabramo/requests-unixsocket - origin of this project.
- https://github.com/httpie/httpie-unixsocket - a plugin for `HTTPie <https://httpie.org/>`_ that allows you to interact with UNIX domain sockets.
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/thelabnyc/requests-unixsocket2",
"name": "requests-unixsocket2",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0.0,>=3.8.1",
"maintainer_email": null,
"keywords": null,
"author": "thelab",
"author_email": "thelabdev@thelab.co",
"download_url": "https://files.pythonhosted.org/packages/14/66/88737c8720685f44e6a1c04cb2185301a6ec4538ac82324f0f33c9dc5fd5/requests_unixsocket2-0.4.2.tar.gz",
"platform": null,
"description": "# requests-unixsocket2\n\n<a href=\"https://badge.fury.io/py/requests-unixsocket2\">\n <img src=\"https://badge.fury.io/py/requests-unixsocket2.svg\" alt=\"Latest Version on PyPI\" />\n</a>\n<a href=\"https://gitlab.com/thelabnyc/requests-unixsocket2/-/commits/master\">\n <img alt=\"pipeline status\" src=\"https://gitlab.com/thelabnyc/requests-unixsocket2/badges/master/pipeline.svg\" />\n</a>\n<a href=\"https://gitlab.com/thelabnyc/requests-unixsocket2/-/commits/master\">\n <img alt=\"coverage report\" src=\"https://gitlab.com/thelabnyc/requests-unixsocket2/badges/master/coverage.svg\" />\n</a>\n<a href=\"https://gitlab.com/thelabnyc/requests-unixsocket2/-/releases\">\n <img alt=\"Latest Release\" src=\"https://gitlab.com/thelabnyc/requests-unixsocket2/-/badges/release.svg\" />\n</a>\n\nUse [requests](http://docs.python-requests.org/) to talk HTTP via a UNIX domain socket.\n\n## Usage\n\n### Explicit\n\nYou can use it by instantiating a special `Session` object:\n\n```py\nimport json\n\nimport requests_unixsocket\n\nsession = requests_unixsocket.Session()\n\nr = session.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')\nregistry_config = r.json()['RegistryConfig']\nprint(json.dumps(registry_config, indent=4))\n```\n\n\n### Implicit (monkeypatching)\n\nMonkeypatching allows you to use the functionality in this module, while making minimal changes to your code. Note that in the above example we had to instantiate a special `requests_unixsocket.Session` object and call the `get` method on that object. Calling `requests.get(url)` (the easiest way to use requests and probably very common), would not work. But we can make it work by doing monkeypatching.\n\nYou can monkeypatch globally:\n\n```py\nimport requests_unixsocket\n\nrequests_unixsocket.monkeypatch()\n\nr = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')\nassert r.status_code == 200\n```\n\nor you can do it temporarily using a context manager:\n\n```py\nimport requests_unixsocket\n\nwith requests_unixsocket.monkeypatch():\n r = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')\n assert r.status_code == 200\n```\n\n### Abstract namespace sockets\n\nTo connect to an [abstract namespace socket](https://utcc.utoronto.ca/~cks/space/blog/python/AbstractUnixSocketsAndPeercred) (Linux only), prefix the name with a NULL byte (i.e.: `\\0`) - e.g.:\n\n```py\nimport requests_unixsocket\n\nsession = requests_unixsocket.Session()\nres = session.get('http+unix://\\0test_socket/get')\nprint(res.text)\n```\n\nFor an example program that illustrates this, see `examples/abstract_namespace.py` in the git repo. Since abstract namespace sockets are specific to Linux, the program will only work on Linux.\n\n## See also\n\n- https://github.com/msabramo/requests-unixsocket - origin of this project.\n- https://github.com/httpie/httpie-unixsocket - a plugin for `HTTPie <https://httpie.org/>`_ that allows you to interact with UNIX domain sockets.\n\n",
"bugtrack_url": null,
"license": "ISC",
"summary": "Use requests to talk HTTP via a UNIX domain socket",
"version": "0.4.2",
"project_urls": {
"Homepage": "https://gitlab.com/thelabnyc/requests-unixsocket2",
"Repository": "https://gitlab.com/thelabnyc/requests-unixsocket2"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4334b1072b2b1310572d8b801adcf3b148197eea2f8207f3750f73fcd2a6db5d",
"md5": "1645389d9099dac77c882ecaefa07433",
"sha256": "701fcd49d74bc0f759bbe45c4dfda0045fd89652948c2b473b1a312214c3770b"
},
"downloads": -1,
"filename": "requests_unixsocket2-0.4.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1645389d9099dac77c882ecaefa07433",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0.0,>=3.8.1",
"size": 7786,
"upload_time": "2024-08-31T18:43:24",
"upload_time_iso_8601": "2024-08-31T18:43:24.294433Z",
"url": "https://files.pythonhosted.org/packages/43/34/b1072b2b1310572d8b801adcf3b148197eea2f8207f3750f73fcd2a6db5d/requests_unixsocket2-0.4.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "146688737c8720685f44e6a1c04cb2185301a6ec4538ac82324f0f33c9dc5fd5",
"md5": "f55a79dd0f8065d9918b571cf4c69331",
"sha256": "929c58ecc5981f3d127661ceb9ec8c76e0f08d31c52e44ab1462ac0dcd55b5f5"
},
"downloads": -1,
"filename": "requests_unixsocket2-0.4.2.tar.gz",
"has_sig": false,
"md5_digest": "f55a79dd0f8065d9918b571cf4c69331",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0.0,>=3.8.1",
"size": 6367,
"upload_time": "2024-08-31T18:43:25",
"upload_time_iso_8601": "2024-08-31T18:43:25.553083Z",
"url": "https://files.pythonhosted.org/packages/14/66/88737c8720685f44e6a1c04cb2185301a6ec4538ac82324f0f33c9dc5fd5/requests_unixsocket2-0.4.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-31 18:43:25",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "thelabnyc",
"gitlab_project": "requests-unixsocket2",
"lcname": "requests-unixsocket2"
}