пытаюсь читать из сокета. иногда сервер перезапускается и это отслеживаю отправкой сообщения в сокет (отправка своего рода пинга, в ответ получаю понг). перезапускаю реконнект к сокету но данные уже не читаются. что не так?
host = '192.168.100.45' clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #clientSocket.setblocking(0) #clientSocket.settimeout(2) #fcntl.fcntl(clientSocket, fcntl.F_SETFL, os.O_NONBLOCK) #clientSocket.connect( ( host, 8902 ) ) #------------------------------------------------------------------------------------------------------------------------------- class pingTimer(): def __init__(self,t,hFunction): self.t=t self.hFunction = hFunction self.thread = Timer(self.t,self.handle_function) def handle_function(self): self.hFunction() self.thread = Timer(self.t,self.handle_function) self.thread.start() def start(self): self.thread.start() def cancel(self): self.thread.cancel() #------------------------------------------------------------------------------------------------------------------------------- def send_ping(): # global clientSocket print ("send ping") try: clientSocket.send(b'33333{"cmd":"ping","rid":"RID000000"}') except: print("error send ping, socket close") clientSocket.close() connect() # timer.start() #------------------------------------------------------------------------------------------------------------------------------- def connect(): global clientSocket try: print( "try to connected to device" ) clientSocket.connect( ( host, 8902 ) ) clientSocket.listen(1) # fcntl.fcntl(clientSocket, fcntl.F_SETFL, os.O_NONBLOCK) connected = True print ("connected") except socket.error: connected = False clientSocket = socket.socket() print( "connection lost... reconnecting" ) while not connected: try: clientSocket.connect( ( host, 8902 ) ) connected = True print( "re-connection successful" ) except socket.error: sleep(2) print("try to reconnect.....") #------------------------------------------------------------------------------------------------------------------------------- def recv_msg(): global clientSocket while True: recv_msg = clientSocket.recv(1024) if not recv_msg: sys.exit(0) recv_msg = recv_msg.decode() print('message recive') print(recv_msg) #------------------------------------------------------------------------------------------------------------------------------- # main start #connect() t_ping = pingTimer(5,send_ping) t_ping.start() #t_listen = threading.Thread(target = recv_msg) connect() #t_listen.start() while True: recv_msg() # print("while1") # print ('recv')