When I connect to the telnet session using the telnetlib
module, i need to wait for four strings: 'a', 'b', 'c' and 'd' or timeout (10 seconds) before I write a string to the socket.
当我使用telnetlib模块连接到telnet会话时,我需要等待4个字符串:'a'、'b'、'c'和'd'或超时(10秒),然后才能向套接字写入字符串。
Is there a way to use tn.read_until('a','b','c','d', timeout)
是否有办法使用tn.read_until('a','b','c','d',超时)
I just want to wait for all 4 strings to come first before action.
我只是想等待所有的4个字符串在操作之前先来。
Also these four strings comes in a different order every time. Thanks for any help.
这四根弦每次都以不同的顺序出现。感谢任何帮助。
1 个解决方案
#1
1
You can use the .expect
method to wait for a
, b
, c
or d
您可以使用.expect方法等待a、b、c或d
Telnet.expect(list[, timeout])
Telnet。期望(列表(超时))
Read until one from a list of a regular expressions matches.
从正则表达式匹配的列表中读取。
So:
所以:
(index, match, content_including_abcd) = tn.expect(['a', 'b', 'c', 'd'], timeout)
Returns (-1, None, current_buffer)
when timeout is reached.
当到达超时时,返回(-1,None, current_buffer)。
We could easily change it to a loop to wait for a
, b
, c
and d
:
我们可以很容易地把它变成一个循环,等待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]
#1
1
You can use the .expect
method to wait for a
, b
, c
or d
您可以使用.expect方法等待a、b、c或d
Telnet.expect(list[, timeout])
Telnet。期望(列表(超时))
Read until one from a list of a regular expressions matches.
从正则表达式匹配的列表中读取。
So:
所以:
(index, match, content_including_abcd) = tn.expect(['a', 'b', 'c', 'd'], timeout)
Returns (-1, None, current_buffer)
when timeout is reached.
当到达超时时,返回(-1,None, current_buffer)。
We could easily change it to a loop to wait for a
, b
, c
and d
:
我们可以很容易地把它变成一个循环,等待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]