Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
examples/ | H | 15-Aug-2019 | - | 60 | 33 | |
requests_unixsocket/ | H | 15-Aug-2019 | - | 388 | 283 | |
requests_unixsocket.egg-info/ | H | 03-May-2022 | - | 115 | 79 | |
.travis.yml | H A D | 15-Aug-2019 | 475 | 26 | 23 | |
AUTHORS | H A D | 15-Aug-2019 | 420 | 12 | 11 | |
ChangeLog | H A D | 15-Aug-2019 | 2.7 KiB | 106 | 91 | |
LICENSE | H A D | 13-Aug-2019 | 11.1 KiB | 203 | 169 | |
Makefile | H A D | 13-Aug-2019 | 204 | 13 | 9 | |
PKG-INFO | H A D | 15-Aug-2019 | 4.1 KiB | 114 | 78 | |
README.rst | H A D | 13-Aug-2019 | 2.5 KiB | 90 | 56 | |
Vagrantfile | H A D | 13-Aug-2019 | 1.2 KiB | 26 | 24 | |
pytest.ini | H A D | 13-Aug-2019 | 37 | 3 | 2 | |
setup.cfg | H A D | 15-Aug-2019 | 928 | 35 | 30 | |
setup.py | H A D | 13-Aug-2019 | 104 | 9 | 5 | |
tox.ini | H A D | 15-Aug-2019 | 1 KiB | 49 | 40 |
README.rst
1requests-unixsocket 2=================== 3 4.. image:: https://badge.fury.io/py/requests-unixsocket.svg 5 :target: https://badge.fury.io/py/requests-unixsocket 6 :alt: Latest Version on PyPI 7 8.. image:: https://travis-ci.org/msabramo/requests-unixsocket.svg?branch=master 9 :target: https://travis-ci.org/msabramo/requests-unixsocket 10 11Use `requests <http://docs.python-requests.org/>`_ to talk HTTP via a UNIX domain socket 12 13Usage 14----- 15 16Explicit 17++++++++ 18 19You can use it by instantiating a special ``Session`` object: 20 21.. code-block:: python 22 23 import json 24 25 import requests_unixsocket 26 27 session = requests_unixsocket.Session() 28 29 r = session.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info') 30 registry_config = r.json()['RegistryConfig'] 31 print(json.dumps(registry_config, indent=4)) 32 33 34Implicit (monkeypatching) 35+++++++++++++++++++++++++ 36 37Monkeypatching allows you to use the functionality in this module, while making 38minimal changes to your code. Note that in the above example we had to 39instantiate a special ``requests_unixsocket.Session`` object and call the 40``get`` method on that object. Calling ``requests.get(url)`` (the easiest way 41to use requests and probably very common), would not work. But we can make it 42work by doing monkeypatching. 43 44You can monkeypatch globally: 45 46.. code-block:: python 47 48 import requests_unixsocket 49 50 requests_unixsocket.monkeypatch() 51 52 r = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info') 53 assert r.status_code == 200 54 55or you can do it temporarily using a context manager: 56 57.. code-block:: python 58 59 import requests_unixsocket 60 61 with requests_unixsocket.monkeypatch(): 62 r = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info') 63 assert r.status_code == 200 64 65 66Abstract namespace sockets 67++++++++++++++++++++++++++ 68 69To connect to an `abstract namespace 70socket <https://utcc.utoronto.ca/~cks/space/blog/python/AbstractUnixSocketsAndPeercred>`_ 71(Linux only), prefix the name with a NULL byte (i.e.: `\0`) - e.g.: 72 73.. code-block:: python 74 75 import requests_unixsocket 76 77 session = requests_unixsocket.Session() 78 res = session.get('http+unix://\0test_socket/get') 79 print(res.text) 80 81For an example program that illustrates this, see 82``examples/abstract_namespace.py`` in the git repo. Since abstract namespace 83sockets are specific to Linux, the program will only work on Linux. 84 85 86See also 87-------- 88 89- https://github.com/httpie/httpie-unixsocket - a plugin for `HTTPie <https://httpie.org/>`_ that allows you to interact with UNIX domain sockets 90