【发布时间】:2017-03-11 14:26:32
【问题描述】:
当我使用telnetlib 模块连接到 telnet 会话时,我需要等待四个字符串:'a'、'b'、'c' 和 'd' 或超时(10 秒),然后才能编写字符串到套接字。
有没有办法使用tn.read_until('a','b','c','d', timeout)
我只想等待所有 4 个字符串都排在前面,然后再执行操作。
这四个字符串每次都以不同的顺序出现。感谢您的帮助。
【问题讨论】:
当我使用telnetlib 模块连接到 telnet 会话时,我需要等待四个字符串:'a'、'b'、'c' 和 'd' 或超时(10 秒),然后才能编写字符串到套接字。
有没有办法使用tn.read_until('a','b','c','d', timeout)
我只想等待所有 4 个字符串都排在前面,然后再执行操作。
这四个字符串每次都以不同的顺序出现。感谢您的帮助。
【问题讨论】:
您可以使用the .expect method等待a、b、c或d
Telnet.期望(列表[, 超时])
读取直到匹配正则表达式列表中的一个。
所以:
(index, match, content_including_abcd) = tn.expect(['a', 'b', 'c', 'd'], timeout)
达到超时时返回(-1, None, current_buffer)。
我们可以轻松地将其更改为循环以等待a、b、c 和 d:
deadline = time.time() + timeout
remaining_strings = ['a', 'b', 'c', 'd']
total_content = ''
while remaining_strings:
actual_timeout = deadline - time.time()
if actual_timeout < 0:
break
(index, match, content) = tn.expect(remaining_strings, actual_timeout)
total_content += content
if index < 0:
break
del remaining_strings[index]
【讨论】: