8 def __init__(self, config):
10 self.port = int(config.get("Comm", "port"))
13 addr = (self.host, self.port)
14 log.debug("Setup TCP server at %s:%s" % addr)
15 self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
16 self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19 log.info("TCP server listening at %s:%s (sock=%d)" %
20 (self.host, self.port, self.tcp.fileno()))
28 if not c or c == "\n":
31 log.error("Error reading message from client: %s" % e)
34 if not bytes or bytes[-1] != "\n":
36 log.error("Invalid message from client: %r" % msg)
45 log.debug("RECV: %r" % msg)
48 def sendMsg(self, msg):
49 log.debug("SEND: %r" % msg)
50 self.con.send(msg + "\n")
52 def sendOk(self, payload=None):
53 self.sendMsg("OK %d" % bool(payload is not None))
54 if payload is not None:
55 if not isinstance(payload, (tuple, list)):
58 self.sendMsg("+%s" % e)
61 def sendNotOk(self, reason=""):
62 self.sendMsg("NOTOK %r" % reason)
65 log.debug("Wait for client request at %s:%s (sock=%d)" %
66 (self.host, self.port, self.tcp.fileno()))
67 self.con, self.client = self.tcp.accept()
68 log.info("Incoming request from %s (con=%s)" %
69 (self.client, self.con.fileno()))
70 return (self.con, self.client, self.port)
72 def disconnect_client(self, connection):
73 log.info("Closed request from %s (con=%s)" %
74 (self.client, self.con.fileno()))