我刚刚开始在python中编码。我想在端口号8888上与telnet客户端建立连接。问题是,当我在命令提示符中输入单个字符时,它显示,与主机的连接丢失。我该怎么办?
import socket
import sys
HOST = ''
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket Started'
try:
s.bind((HOST,PORT))
except socket.error, msg:
print 'Bind Failed'
sys.exit()
print 'Socket Bind Complete'
s.listen(10)
print 'Socket now listning'
conn,addr = s.accept()
print 'Connected with ' +addr[0] + ' : ' +str(addr[1])
data = conn.recv(4096)
conn.sendall(data)
conn.close()
答案 0 :(得分:1)
如果您想打印数据,请在收到后立即执行:
#...
data = conn.recv(4096)
print "received data:", data
#...