12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import socket
- import threading
- import config as cfg
- import json
- LenFunc = cfg.LenFunc
- class Socket(threading.Thread):
- def __init__(self, HOST,PORT,data):
- super(Socket,self).__init__(None,None,None,None)
- self.port = PORT
- self.Host = HOST
- self.history = []
- self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.socket.connect((HOST, PORT))
- t = threading.Thread(target=self.receive)
- t.start()
- print("socket is created and Listener is created\n")
- self.toSEND(data)
- def send(self,Message):
- Message=bytes(Message,'utf-8')
- self.socket.sendall(Message)
- # self.history.append(("send",Message))
- print("Send",Message)
- def receive(self):
- while True:
- len = self.socket.recv(LenFunc)
- if not len:
- break
- data = self.socket.recv(int(len))
- print("data",data)
- # self.history.append(("receive",data))
- def toJSON(self, data):
- return json.loads(data)
- def toSTRING(self, data):
- return json.dumps(data)
- def toSEND(self, x):
- j = json.dumps(x)
- l = LenFunc - len(str(len(j)))
- self.send("0000"[0:l]+str(len(j))+j)
- def parser(self,data):
- print("parser not yet implemented. ",data)
|