我已经从教程中创建了一个客户端服务器设置,但服务器卡在下面的while循环中,我无法弄清楚原因。 我在这里搜索了一些问题,据我所知它应该有用。
我相信这会很简单。我是Python的新手,所以任何帮助都会受到赞赏。 请知道这是我的第一个问题,所以我希望它能正常工作。
我正在使用python 3.5.2
# create a client thread
def clientthread(conn):
# sending message to the client
message = 'Welcome to the Server. Type something and hit Enter\n'
conn.send(message.encode()) # send only takes a string
while 1:
# receiving from client
data = conn.recv(1024)
if not data.decode(): break
reply = 'OK ' + data.decode()
conn.sendall(reply.encode())
# came out of loop
conn.close()
# keep server up
while 1:
# accept incomming connections
conn, addr = soc.accept()
# display client info
print('Connected with' + addr[0] + ':' + str(addr[1]))
# start a new thread takes function name and a tupal
start_new_thread(clientthread, (conn, ))
soc.close()
答案 0 :(得分:0)
想出了问题,我使用telnet来测试服务器,它在服务器看到的数据末端添加了一个\ r \ n,因此永远不会关闭。添加了代码来检查这个并且现在正常工作