저는 파이썬 소켓에 대해 배우고 있으며 telnet xxx.xxx.xxx.xxx 8888
을 사용하여 폴링 할 수있는 하나의 컴퓨터에 짧은 서버 소켓 프로그램을 작성했습니다. 통신은 정상적으로 작동하지만 클라이언트에서 보낸 data
을 평가할 때 어떤 이유로 클라이언트가 평가 한 것처럼 보이지 않습니다.if 문을 사용하여 Python 소켓 데이터가 예기치 않게 작동합니다.
기분을 상하게하는 코드는 다음과 같습니다 : 나는 다음을 수행 클라이언트에서
data = conn.recv(1024) # receive data
conn.sendall(data) # reply to confirm what data was received
if data == "green":
reply = 'GREEN ON'
elif data == "red":
reply = 'RED ON'
else:
reply = 'Command not recognized'
conn.sendall(reply)
:
telnet 192.168.0.8 8888
Trying 192.168.0.8...
Connected to 192.168.0.8.
Escape character is '^]'.
Welcome to the server. Type something and hit enter # server responds with connection confirmation
green #typed from client
green #server responds with the data it received
Command not recognized #<-- data is not evaluated correctly in if statement
그래서 일어나고있는 것은 내가 Welcome to the server...
가를 나타내는 응답 telnet
를 사용하여 서버를 폴링입니다 성공적인 연결. 그런 다음 green
을 입력하면 서버가받은 데이터로 응답합니다 (즉, green
). 그런 다음 서버 프로그램의 if
문이 data
을 올바르게 평가하지 못했다는 의미로 명령이 인식되지 않는다고 알려줍니다.
데이터가 실제로 누락 되었습니까? green\0
과 같이 null로 끝나는 것처럼 if
블록에서 잘못된 평가가 발생합니까?
답장을 보내 주셔서 감사합니다. 나는 그 코드 행을'if data == "green \ n"'로 읽었지만 여전히 동일하게 변경했다. – nagyben
@exantas : 실제로받은 것을보기 위해'print (repr (data)) '문장을 넣으십시오. 나는 그것이 '\ r \ n' 대'\ n' 문제 일 것이지만, 실제로 추측 할 이유가 없습니다. – abarnert
당신이 맞았습니다. 실제로'녹색 \ r \ n'을 받았습니다. 그래도 좋은 팁입니다. 감사합니다! – nagyben