]> defiant.homedns.org Git - pyshared.git/commitdiff
added json_server
authorErik Andresen <erik@vontaene.de>
Sat, 3 Jan 2015 11:07:37 +0000 (12:07 +0100)
committerErik Andresen <erik@vontaene.de>
Sat, 3 Jan 2015 11:07:37 +0000 (12:07 +0100)
.gitignore [new file with mode: 0644]
json_server.py [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..c9b568f
--- /dev/null
@@ -0,0 +1,2 @@
+*.pyc
+*.swp
diff --git a/json_server.py b/json_server.py
new file mode 100755 (executable)
index 0000000..081a499
--- /dev/null
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+# -*- coding: iso-8859-15 -*-
+
+import SocketServer
+import json
+import threading
+import logging
+import control
+import threading
+
+SocketServer.TCPServer.allow_reuse_address = True
+
+logger = logging.getLogger(__name__)
+
+class TCPServer(threading.Thread):
+       def __init__(self):
+               threading.Thread.__init__(self)
+               self.setDaemon(True)
+               self.server = SocketServer.ThreadingTCPServer(('0.0.0.0', 10002), TCPHandler)
+               self.server.allow_reuse_address = True
+               self.server.daemon_threads = True
+               self.start()
+
+       def run(self):
+               self.server.serve_forever()
+
+       def stop(self):
+               self.server.shutdown()
+               self.server.socket.close()
+
+
+class TCPHandler(SocketServer.BaseRequestHandler):
+       def __init__(self, request, client_address, server):
+               self.__lock_send = threading.Lock()
+               SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
+
+       def finish(self):
+               return SocketServer.BaseRequestHandler.finish(self)
+
+       def send_log(self, s):
+               self.send({"log": s})
+
+       def send(self, d):
+               with self.__lock_send:
+                       self.request.sendall(json.dumps(d))
+
+       def handle(self):
+               try:
+                       while True:
+                               s = self.request.recv(4096)
+                               if not s:
+                                       return
+
+                               data = json.loads(s)
+                               if data.has_key("command"):
+                                       cmd = data["command"]
+                                       ret = control.handle(cmd)
+                                       self.send({"return": ret, "command_was": cmd})
+
+               except Exception, e:
+                       logger.debug("Exception wile receiving message: \n%s", e)
+
+
+if __name__ == "__main__":
+       from time import sleep
+
+       logger.setLevel(logging.DEBUG)
+       logger.addHandler(logging.StreamHandler())
+
+       pServer = TCPServer()
+       while True:
+               sleep(1)