Source

puma / mdc-telnet.py

#!/usr/bin/python
import SocketServer
import MDC
import threading
import time

# Force SOREUSEADDR allways
SocketServer.TCPServer.allow_reuse_address = True

controller = MDC.MDC("/dev/ttys005")

class MDCSocket(SocketServer.StreamRequestHandler):
	def handle(self):
		self.timeout = 1
		self.thread = threading.Thread(target = self.output_periodic)
		self.thread.daemon = True
		self.thread.start()

		while True:
			line = self.rfile.readline()
			if not line:
				break
			print "Read line" + line

		self.thread.stop()
	
	def output_periodic(self):
		while True:
			self.wfile.write(','.join(MDC.get_counts()) + '\n')
			time.sleep(1)

if __name__ == "__main__":
	host = 'localhost'
	port = 9999
	server = SocketServer.TCPServer((host,port), MDCSocket)
	server.serve_forever()