【发布时间】:2019-01-10 15:12:19
【问题描述】:
我创建了一个回显服务器,用于侦听传入连接并回显任何接收到的数据。我正在使用 telnet 建立连接。
#!/usr/bin/env python
import socket
import sys
# Create socket
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Port for socket and Host
PORT = 8001
HOST = 'localhost'
# bind the socket to host and port
sockfd.bind((HOST, PORT))
# become a server socket
sockfd.listen(5)
while True:
# Establish and accept connections woth client
(clientsocket, address) = sockfd.accept()
print("Got connection from", address)
# Recieve message from the client
message = clientsocket.recv(1024)
reply = 'Server output: ' + message.decode('utf-8')
if not message:
break
# Display messags.
clientsocket.sendall(str.encode(reply))
# Close the connection with the client
clientsocket.close()
目前在我提示“服务器输出:”后,它挂起并且没有任何内容回显到终端。
【问题讨论】: