Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Supersimple proxy for graphite allowing to inject data without timestamp.
#!/usr/bin/env python2
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import socket
import time
class UplinkConnection:
def __init__(self, host, port):
self.host = host
self.port = port
self._socket = None
self._reconnect()
def _reconnect(self):
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.connect((self.host, self.port))
def send(self, path, value, timestamp):
try:
send_string = "{path} {value} {timestamp}\n".format(path=path, value=value, timestamp=timestamp)
print(send_string)
self._socket.sendall(send_string)
except socket.error:
self._reconnect()
class Chat(LineReceiver):
def connectionMade(self):
print("New connection from {}".format(self))
def lineReceived(self, line):
global uplink
try:
path, value = line.split(' ', 2)
timestamp = int(time.time())
uplink.send(path, value, timestamp)
except ValueError:
pass
class ChatFactory(Factory):
def buildProtocol(self, addr):
return Chat()
if __name__ == "__main__":
uplink = UplinkConnection("127.0.0.1", 2003)
reactor.listenTCP(2005, ChatFactory())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.