summaryrefslogtreecommitdiff
path: root/lavaproxy/socketserver.py
diff options
context:
space:
mode:
authorMatt Hart <matthew.hart@linaro.org>2014-04-18 17:43:41 +0100
committerMatt Hart <matthew.hart@linaro.org>2014-04-18 17:43:41 +0100
commitffb5edd271230606407c5ebe4c708cd637ea7436 (patch)
tree4183ac39b7e348a660fcae2ce7e134e1a3889f03 /lavaproxy/socketserver.py
parentbc8b057384ebc289f6f708818c78690b4c4d36d4 (diff)
Renamed folder and cut down socketserver.py to remove the lavapdu specific stuff
Diffstat (limited to 'lavaproxy/socketserver.py')
-rw-r--r--lavaproxy/socketserver.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/lavaproxy/socketserver.py b/lavaproxy/socketserver.py
new file mode 100644
index 0000000..a678207
--- /dev/null
+++ b/lavaproxy/socketserver.py
@@ -0,0 +1,80 @@
+#! /usr/bin/python
+
+# Copyright 2013 Linaro Limited
+# Author Matt Hart <matthew.hart@linaro.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+import SocketServer
+import logging
+import socket
+
+class ListenerServer(object):
+ servers = []
+
+ def __init__(self, config):
+ logging.getLogger().name = "ManagementServer"
+ logging.getLogger().setLevel(config["logging_level"])
+ self.management_server = TCPServer((config["hostname"], config["mgmt-port"]), TCPRequestHandler)
+# for portnum in range(config["port-start"], config["port-end"]):
+# self.servers.append(TCPServer((config["hostname"], portnum), TCPRequestHandler))
+# logging.info("listening on %s:%s" % (config["hostname"], portnum))
+
+ def start(self):
+ logging.info("Starting the Management Server")
+ self.management_server.serve_forever()
+ # for server in self.servers:
+ # server.serve_forever()
+
+
+class TCPRequestHandler(SocketServer.BaseRequestHandler):
+ #"One instance per connection. Override handle(self) to customize action."
+ def handle(self):
+ logging.getLogger().name = "TCPRequestHandler"
+ ip = self.client_address[0]
+ logging.debug(self.client_address)
+ try:
+ data = self.request.recv(4096).strip()
+ socket.setdefaulttimeout(2)
+ try:
+ request_host = socket.gethostbyaddr(ip)[0]
+ except socket.herror as e:
+ logging.debug("Unable to resolve: %s error: %s" % (ip, e))
+ request_host = ip
+ logging.info("Received a request from %s: '%s'" % (request_host, data))
+ self.request.sendall("ack\n")
+ except Exception as e:
+ logging.debug(e)
+ self.request.sendall("nack\n")
+ self.request.close()
+
+
+class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
+ allow_reuse_address = True
+ daemon_threads = True
+ pass
+
+if __name__ == "__main__":
+ logging.basicConfig(level=logging.DEBUG)
+ logging.getLogger().setLevel(logging.DEBUG)
+ logging.debug("Executing from __main__")
+ starter = {"hostname": "0.0.0.0",
+ "port-start": 40001,
+ "port-end": 41000,
+ "mgmt-port": 40000,
+ "logging_level": logging.DEBUG}
+ ss = ListenerServer(starter)
+ ss.start()