简体   繁体   中英

Python socket server: can't compare input from client

I'm practicing a little bit with constructing a socket server on python. I'm currently running it locally on my linux mint system, and testing it with telnet localhost 20000 .

The idea is quite simple. For now, I want the client(myself) to send a "hello" message to the server on which the server responses back with another "hello" message. Then the client can send any message on which the server does not respond, until the client says "bye". Then the server responds with another "bye", and the socket is closed.

I implemented this in the following way (in python 3):

  1 from socket import *
  2 serverPort = 20000
  3 serverSocket = socket(AF_INET, SOCK_STREAM)
  4 serverSocket.bind(('', serverPort))
  5 serverSocket.listen(1)
  6 
  7 print('The echo server is ready to receive')
  8 while 1:
  9     connectionSocket, addr = serverSocket.accept()
 10     print('Processing client ', addr)
 11     sentence = ""
 12 
 13     try:
 14         sentence = connectionSocket.recv(1024)
 15         while sentence != "hello":
 16             sentence = connectionSocket.recv(1024)
 17         connectionSocket.send("hello")
 18 
 19         sentence = connectionSocket.recv(1024)
 20         while sentence != "bye":
 21             sentence = connectionSocket.recv(1024)
 22         connectionSocket.send("bye")
 23     except error:
 24         pass
 25 
 26     print('Client closed ', addr)
 27     connectionSocket.close()

It's a very simple program, in which I can't find any bug, so I was very surprised to see it not work. When I type in "hello" after running the server with python3 server.py and establishing a connection trough telnet localhost 20000 I simply get no response. I first thought that the problem lies in the equality test in line number 15, so I tested a few things with prints, and instead of "hello" The message received by the server was: b'hello\\r\\n. I understand the \\n, because I type Enter after the "hello" message, but it's not clear to me why the b' and the \\r appear.

Do you know what the problem is here and how I can fix it? On other examples on stack overflow the string comparision just works fine, so I can't figure out why it is not working for me.

Thanks in advance!

Update: I use nc now instead of telnet, and changed every occurence of connectionSocket.recv(1024) into str(connectionSocket.recv(1024), 'utf-8'). The server gets the correct string now, but it still does not echo "hello" back to me, nor does it close the socket when I type "bye".

Use netcat instead of telnet to avoid the telnet protocol bytes. Like this: nc localhost 20000 and type your input then.

Also, socket I/O is always bytes in Python 3, for instance see https://docs.python.org/3/library/socket.html#socket.socket.recv So you have to compare with b'hello' (or decode the bytes to a string first)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM