Python 可以使用pexpect获取输出直到超时,而不查找任何特定模式吗
我已经生成了一个telnet会话。我想得到会话在给定时间间隔内抛出的任何输出。如何使用pexpect实现它。 我尝试了expect('.*',20)。但这将匹配在此时间之前输出的任何内容,并且不会等待超时20秒Python 可以使用pexpect获取输出直到超时,而不查找任何特定模式吗,python,Python,我已经生成了一个telnet会话。我想得到会话在给定时间间隔内抛出的任何输出。如何使用pexpect实现它。 我尝试了expect('.*',20)。但这将匹配在此时间之前输出的任何内容,并且不会等待超时20秒 # set expected to some string you might not see def ReadByDuration(connection, expected = ".*", read_time = 20, epsilon = 0.001): from time
# set expected to some string you might not see
def ReadByDuration(connection, expected = ".*", read_time = 20, epsilon = 0.001):
from time import time
start_time = time()
response = connection.read_until(expected,float(read_time))
time_passed = time() - start_time
# if timeout not met, read again
while read_time - time_passed > epsilon :
response += connection.read_until(expected, float(read_time - time_passed))
time_passed = time() - start_time
return response
如果你想变得更有趣,你可以把它添加到类函数中
import telnetlib, types
t = telnetlib.Telnet(IP, PORT, 20)
t.ReadByDuration = types.MethodType( ReadByDuration, t )
t.ReadByDuration(".*", 20)