Created
December 30, 2021 11:28
-
-
Save fphammerle/5a23717cceff3e724a512c3ded6e9b53 to your computer and use it in GitHub Desktop.
Send HTTP request to unix socket with python's built-in http.client.HttpConnection class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tested on cpython v3.9.5 | |
import http.client | |
import json | |
import pathlib | |
import socket | |
class _HttpConnectionUnixSocket(http.client.HTTPConnection): | |
def __init__(self, path: pathlib.Path) -> None: | |
self._path = path | |
super().__init__(host="0.0.0.0") | |
def connect(self) -> None: | |
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
self.sock.connect(self._path.as_posix()) | |
docker_api_connection = _HttpConnectionUnixSocket(pathlib.Path("/var/run/docker.sock")) | |
docker_api_connection.request(method="GET", url="/v1.24/info") | |
print(json.load(docker_api_connection.getresponse())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment