• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H15-Aug-2019-6033

requests_unixsocket/H15-Aug-2019-388283

requests_unixsocket.egg-info/H03-May-2022-11579

.travis.ymlH A D15-Aug-2019475 2623

AUTHORSH A D15-Aug-2019420 1211

ChangeLogH A D15-Aug-20192.7 KiB10691

LICENSEH A D13-Aug-201911.1 KiB203169

MakefileH A D13-Aug-2019204 139

PKG-INFOH A D15-Aug-20194.1 KiB11478

README.rstH A D13-Aug-20192.5 KiB9056

VagrantfileH A D13-Aug-20191.2 KiB2624

pytest.iniH A D13-Aug-201937 32

setup.cfgH A D15-Aug-2019928 3530

setup.pyH A D13-Aug-2019104 95

tox.iniH A D15-Aug-20191 KiB4940

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