Skip to content

Instantly share code, notes, and snippets.

@ScheerMT
Last active September 16, 2015 15:50
Show Gist options
  • Save ScheerMT/d5ddad9a2bbdf2ef3f71 to your computer and use it in GitHub Desktop.
Save ScheerMT/d5ddad9a2bbdf2ef3f71 to your computer and use it in GitHub Desktop.
Implemented the ruby version in python to handle multiple responses in a single packet. Some client implementations of statsd buffer packets together to be sent as one UDP packet. *NOTE*: You may have to change your location of the python executable in the sha-bang. I put the contents of this gist in /usr/bin/lstatsd so it can be run in terminal
#!/usr/local/bin/python
import socket
from termcolor import colored
UDP_IP = "0.0.0.0"
UDP_PORT = 8125
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(4096)
print "======START PACKET====="
dataArray = data.splitlines()
for line in dataArray:
metric, value = line.split(':')
print "StatsdD Metric:", colored(metric, "blue"), colored(value, "green")
print "=====END PACKET=====\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment