所以,當我打開了CMD,並創建一個telnet連接:Python的3插槽 - 接收更多的則1個字符
遠程登錄本地主機5555
它將apear「歡迎」,因爲你可以看到下面的屏幕。 之後,我輸入CMD的每個字符都會立即打印出來/發送。 我的問題是:是的,如果是的話,怎麼可能輸入消息,然後發送它們,所以我收到他們作爲一個句子,而不是字符char。
import socket
import sys
from _thread import *
host = ""
port = 5555
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.bind((host,port))
except socket.error as e:
print(str(e))
s.listen(5) #Enable a server to accept connections.
print("Waiting for a connection...")
def threaded_client(conn):
conn.send(str.encode("Welcome\n"))
while True:
# for m in range (0,20): #Disconnects after x chars
data = conn.recv(2048) #Receive data from the socket.
reply = "Server output: "+ data.decode("utf-8")
print(data)
if not data:
break
conn.sendall(str.encode(reply))
conn.close()
while True:
conn, addr = s.accept()
print("connected to: "+addr[0]+":"+str(addr[1]))
start_new_thread(threaded_client,(conn,))
設置Telnet應該讓事情變得更容易。 – Flint