diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/pybind/mgr/telegraf/basesocket.py | |
parent | Initial commit. (diff) | |
download | ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/pybind/mgr/telegraf/basesocket.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/pybind/mgr/telegraf/basesocket.py b/src/pybind/mgr/telegraf/basesocket.py new file mode 100644 index 000000000..435994661 --- /dev/null +++ b/src/pybind/mgr/telegraf/basesocket.py @@ -0,0 +1,45 @@ +import socket + + +class BaseSocket(object): + schemes = { + 'unixgram': (socket.AF_UNIX, socket.SOCK_DGRAM), + 'unix': (socket.AF_UNIX, socket.SOCK_STREAM), + 'tcp': (socket.AF_INET, socket.SOCK_STREAM), + 'tcp6': (socket.AF_INET6, socket.SOCK_STREAM), + 'udp': (socket.AF_INET, socket.SOCK_DGRAM), + 'udp6': (socket.AF_INET6, socket.SOCK_DGRAM), + } + + def __init__(self, url): + self.url = url + + try: + socket_family, socket_type = self.schemes[self.url.scheme] + except KeyError: + raise RuntimeError('Unsupported socket type: %s', self.url.scheme) + + self.sock = socket.socket(family=socket_family, type=socket_type) + if self.sock.family == socket.AF_UNIX: + self.address = self.url.path + else: + self.address = (self.url.hostname, self.url.port) + + def connect(self): + return self.sock.connect(self.address) + + def close(self): + self.sock.close() + + def send(self, data, flags=0): + return self.sock.send(data.encode('utf-8') + b'\n', flags) + + def __del__(self): + self.sock.close() + + def __enter__(self): + self.connect() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.close() |