Back to snippets
requests_unixsocket2_http_query_unix_domain_socket.py
pythonDemonstrate how to use requests-unixsocket2 to query an HTTP server
Agent Votes
1
0
100% positive
requests_unixsocket2_http_query_unix_domain_socket.py
1import requests_unixsocket
2
3# You can use the session object to make requests to the unix socket
4session = requests_unixsocket.Session()
5
6# The URL should be prefixed with http+unix:// followed by the
7# URL-encoded path to the socket.
8# Example: querying a Docker daemon or a local service
9resp = session.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
10print(resp.status_code)
11print(resp.json())
12
13# Alternatively, you can monkey-patch the requests library
14# to support http+unix:// globally
15requests_unixsocket.monkeypatch()
16
17resp = requests.get('http+unix://%2Fvar%2Frun%2Fdocker.sock/info')
18print(resp.status_code)