Python Telnetlib read_until 'a' 和 'b' 和 'c' 和 'd'
Python Telnetlib read_until 'a' and 'b' and 'c' and 'd'
当我使用 telnetlib
模块连接到 telnet 会话时,我需要等待四个字符串:'a'、'b'、'c' 和 'd' 或在我将字符串写入套接字之前超时(10 秒)。
有没有办法使用tn.read_until('a','b','c','d', timeout)
我只想等到所有 4 个字符串都排在第一位后再进行操作。
而且这四个字符串每次出现的顺序都不一样。感谢您的帮助。
您可以使用the .expect
method等待a
、b
、c
或d
Telnet.expect(list[, timeout])
Read until one from a list of a regular expressions matches.
所以:
(index, match, content_including_abcd) = tn.expect(['a', 'b', 'c', 'd'], timeout)
Returns (-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]
当我使用 telnetlib
模块连接到 telnet 会话时,我需要等待四个字符串:'a'、'b'、'c' 和 'd' 或在我将字符串写入套接字之前超时(10 秒)。
有没有办法使用tn.read_until('a','b','c','d', timeout)
我只想等到所有 4 个字符串都排在第一位后再进行操作。
而且这四个字符串每次出现的顺序都不一样。感谢您的帮助。
您可以使用the .expect
method等待a
、b
、c
或d
Telnet.expect(list[, timeout])
Read until one from a list of a regular expressions matches.
所以:
(index, match, content_including_abcd) = tn.expect(['a', 'b', 'c', 'd'], timeout)
Returns (-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]