connection.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import socket
  2. import threading
  3. import json
  4. import config as cfg
  5. class Connection(threading.Thread):
  6. def __init__(self,connection,address,oracleSocketPort,typee):
  7. super(Connection,self).__init__(None, None,
  8. None, None)
  9. self.conn = connection
  10. self.addr = address
  11. self.history = []
  12. self.type = typee
  13. self.oracleSocketPort = oracleSocketPort
  14. t = threading.Thread(target=self.receive)
  15. t.start()
  16. # self.send("Connection")
  17. def toSEND(self, data):
  18. j = json.dumps(x)
  19. l = LenFunc - len(str(len(j)))
  20. self.send("0000"[0:l]+str(len(j))+j)
  21. def send(self,Message):
  22. Message=bytes(Message,'utf-8')
  23. self.conn.sendall(Message)
  24. # self.history.append(("send",Message))
  25. def receive(self):
  26. while True:
  27. len = self.conn.recv(cfg.LenFunc)
  28. if not len:
  29. break
  30. data = self.conn.recv(int(len))
  31. print("data",data)
  32. self.type.parser(data.decode("utf-8"),self.conn)
  33. # self.history.append(("receive",data))