2017-11-24 2 views
0

저는 파이썬에서 asyncore를 사용하여 소켓 TCP 서버를 사용했습니다. 내 handle_read 함수는 다음과 같습니다.Python Asyncore는 클라이언트가 연결을 끊을 때 handle_read를 실행합니다.

def handle_read(self): 

    data = self.recv(50) 

    '''interpretar os comandos: 
    operação: Ligar/Desligar Bomba, Ligar/Desligar Aquecedor, Alterar velocidade da bomba 
    Modo: trocar de modo automático para remoto 
    Armazenamento: ativar ou desativar o armazenamento de dados para o trend e 
    também apagar dados 
    ''' 
    if len(data) < 2: #comandos digitais 
     try: 
      process_commands(data) 
     except Exception as err: 
      print(str(err)) 
    else: #comando analogico 
     try: 
      ld = json.loads(data.decode('utf-8')) 
      bytescommand = pack('f',ld['pump_speed']) 
      bus.write_block_data(arduinoAddress,53,list(bytescommand)) 
     except Exception as err: 
      print(str(err)) 
     finally: 
      pass 

보세요, 함수를 실행하기 위해받은 데이터를 테스트합니다. 그러나 클라이언트 차단, 프로그램은 반환 할 때 :

"문자 형식은 아이폰에 하나의 바이트 객체를 필요로"이것은 handle_read 기능이 때 클라이언트 차단을 실행하는 것을 나타냅니다. 정상입니까? 상황을 어떻게 처리합니까?

고맙습니다.

답변

0

정상적인 통화입니다. 피어가 소켓을 닫으면 (또는 단순히 SH_WR로 종료 할 때) 읽기 호출이 즉시 0 바이트로 반환됩니다.

그래서 handle_read 소켓의 파일의 말을 준비해야합니다

데프 handle_read (자기) :

... 
if len(data) == 0: # EOF on input socket 
    pass   # or add disconnection processing... 
elif len(data) < 2: #comandos digitais 
    ... 
관련 문제