简体   繁体   English

Python telnetlib telnet.write()删除消息

[英]Python telnetlib telnet.write() dropping messages

Given the following code: 给出以下代码:

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))

By looking at the server and also Wiresharking it, only messages 1 and 2 gets through. 通过查看服务器并进行Wiresharking,仅消息1和2通过。

Now, if I change the func2(t,command) function as follows: 现在,如果我按如下所示更改func2(t,command)函数:

def func2(t,command):
    t.write(command.encode('ascii')+b'\n')
    t.read_eager()   #This is the new line.
    print(command)

It all works fine and all messages are been transmitted. 一切正常,所有消息均已发送。

Any idea? 任何想法?

Python3.3 WindowsXP Python3.3 WindowsXP

You need to read the text coming back to prevent the socket blocking. 您需要阅读返回的文字以防止套接字阻塞。 This is why your other method works. 这就是为什么您使用其他方法的原因。

In a real world session you would always be reading back the results of logging in and of commands. 在现实世界的会话中,您总是会读回登录和命令的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM