鑑於以下代碼:的Python telnetlib telnet.write()丟棄消息
import telnetlib
import sys
def func1(IP,user,passw):
t=telnetlib.Telnet(IP)
t.write(user.encode('ascii')+b'\n')
t.write(passw.encode('ascii')+b'\n')
return t
def func2(t,command):
t.write(command.encode('ascii')+b'\n')
print(command)
user=sys.argv[1]
passw=sys.argv[2]
IP=sys.argv[3]
t=func1(IP,user,passw)
for i in range(6):
func2(t, "message "+str(i))
通過查看服務器,並且還執行Wireshark它,只有消息1和2獲得通過。
現在,如果我改變func2(t,command)
功能如下:
def func2(t,command):
t.write(command.encode('ascii')+b'\n')
t.read_eager() #This is the new line.
print(command)
這一切工作正常,所有的消息都被髮送。
有什麼想法?
Python3.3 WindowsXP的
對於初學者來說,你的代碼包含很多語法錯誤。你不能使用'pass'作爲變量名,它是保留的。另外,您的'for'語句中不需要括號。 – aIKid