Skip to content

Commit 4c83310

Browse files
committed
added a (very) basic tcpircbot client class
1 parent 7057ae6 commit 4c83310

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

cassandra/tools/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
from .instances import get_instances
77

8+
89
__all__ = ["get_instances"]

cassandra/tools/tcpircbot.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
import logging
4+
import socket
5+
6+
7+
# Cribbed from: https://phabricator.wikimedia.org/diffusion/MSCA/browse/master/scap/log.py;d407eaf
8+
class IrcBot(object):
9+
def __init__(self, host, port, timeout=1.0):
10+
self.host = host
11+
self.port = port
12+
self.timeout = timeout
13+
14+
def log(self, msg, *args, **kwargs):
15+
sock = None
16+
try:
17+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
18+
sock.settimeout(self.timeout)
19+
sock.connect((self.host, self.port))
20+
sock.sendall(IrcBot.format_message(msg, *args, **kwargs))
21+
except (socket.timeout, socket.error, socket.gaierror), err:
22+
logging.error("Unable to send to logmsgbot (SAL): %s", err.message)
23+
finally:
24+
if sock:
25+
sock.close()
26+
27+
@classmethod
28+
def format_message(cls, msg, *args, **kwargs):
29+
return "!log " + msg.format(*args, **kwargs)

0 commit comments

Comments
 (0)