socket.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import socket
  2. import threading
  3. import config as cfg
  4. from Utils import *
  5. LenFunc = cfg.LenFunc
  6. class SocketHTTP(threading.Thread):
  7. def __init__(self, HOST, PORT, parent):
  8. try:
  9. self.parent = parent
  10. super(SocketHTTP, self).__init__(None, None, None, None)
  11. self.port = PORT
  12. self.Host = HOST
  13. self.history = []
  14. self.socket = socket.socket()
  15. self.socket.connect((HOST, PORT))
  16. t = threading.Thread(target=self.receive)
  17. t.start()
  18. gBackPrint("socket is created and Listener is created", (HOST, PORT))
  19. except Exception as e:
  20. rBackPrint("An exception occurred SocketHTTP __init__", e)
  21. def send(self, M):
  22. try:
  23. self.socket.sendall(M)
  24. self.history.append(("send", M))
  25. vPrint("SocketHTTP Send", M)
  26. except Exception as e:
  27. rBackPrint("An exception occurred SocketHTTP send", e)
  28. def receive(self):
  29. try:
  30. while True:
  31. data = self.socket.recv(65565)
  32. bPrint("receive", data)
  33. if bytes.hex(data) == "":
  34. continue
  35. self.parent.recv(bytes.hex(data))
  36. except Exception as e:
  37. rBackPrint("An exception occurred SocketHTTP receive", e)
  38. class SocketClient(threading.Thread):
  39. def __init__(self, HOST, PORT, parser, id):
  40. try:
  41. self.parser = parser
  42. super(SocketClient, self).__init__(None, None, None, None)
  43. self.port = PORT
  44. self.Host = HOST
  45. self.history = []
  46. self.id = id
  47. self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  48. self.socket.connect((HOST, PORT))
  49. t = threading.Thread(target=self.receive)
  50. t.start()
  51. gBackPrint("socket is created and Listener is created\n")
  52. except Exception as e:
  53. rBackPrint("An exception occurred SocketClient __init__", e)
  54. def send(self, M):
  55. try:
  56. Message = bytes(M, "utf-8")
  57. self.socket.sendall(Message)
  58. self.history.append(("send", Message))
  59. vPrint("SocketHTTP Send", M)
  60. except Exception as e:
  61. rBackPrint("An exception occurred SocketClient send", e)
  62. def receive(self):
  63. try:
  64. while True:
  65. len = self.socket.recv(cfg.LenFunc)
  66. if not len:
  67. break
  68. data = self.socket.recv(int(len.decode("utf-8")))
  69. data = self.parser.toJSON(data.decode("utf-8"))
  70. bPrint("receive", data)
  71. self.parser.parser(data, None, self)
  72. except Exception as e:
  73. rBackPrint("An exception occurred SocketClient receive", e)
  74. class SocketServer:
  75. def __init__(self, socketPort, parser, id):
  76. try:
  77. self.parser = parser
  78. self.history = []
  79. self.connected = []
  80. self.socketPort = socketPort
  81. self.id = id
  82. t = threading.Thread(target=self.connection)
  83. t.start()
  84. gBackPrint("socket is created and Listener is created", socketPort)
  85. except Exception as e:
  86. rBackPrint("An exception occurred SocketServer __init__", e)
  87. def connection(self):
  88. try:
  89. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  90. s.bind(("localhost", self.socketPort))
  91. s.listen()
  92. self.socket = s
  93. while True:
  94. conn, addr = self.socket.accept()
  95. print("user connected to ", conn, "-> ", addr)
  96. u = Connection(conn, addr, self.socketPort + 1, self.parser)
  97. self.connected.append(u)
  98. except Exception as e:
  99. rBackPrint("An exception occurred SocketServer connection", e)
  100. class Connection:
  101. def __init__(self, connection, address, oracleSocketPort, parser):
  102. try:
  103. self.parser = parser
  104. self.socket = connection
  105. self.addr = address
  106. self.history = []
  107. self.type = None
  108. self.oracleSocketPort = oracleSocketPort
  109. t = threading.Thread(target=self.receive)
  110. t.start()
  111. except Exception as e:
  112. rBackPrint("An exception occurred Connection __init__", e)
  113. def send(self, M):
  114. try:
  115. Message = bytes(M, "utf-8")
  116. self.socket.sendall(Message)
  117. self.history.append(("send", Message))
  118. vPrint("Connection Send", M)
  119. except Exception as e:
  120. rBackPrint("An exception occurred Connection send", e)
  121. def receive(self):
  122. try:
  123. while True:
  124. len = self.socket.recv(cfg.LenFunc)
  125. if not len:
  126. break
  127. data = self.socket.recv(int(len.decode("utf-8")))
  128. data = self.parser.toJSON(data.decode("utf-8"))
  129. bPrint("Connection receive", data)
  130. self.parser.parser(data, None, self)
  131. except Exception as e:
  132. rBackPrint("An exception occurred Connection receive", e)