【问题标题】:Python Telnetlib read_until 'a' and 'b' and 'c' and 'd'Python Telnetlib read_until 'a' 和 'b' 和 'c' 和 'd'
【发布时间】:2017-03-11 14:26:32
【问题描述】:

当我使用telnetlib 模块连接到 telnet 会话时,我需要等待四个字符串:'a'、'b'、'c' 和 'd' 或超时(10 秒),然后才能编写字符串到套接字。

有没有办法使用tn.read_until('a','b','c','d', timeout)

我只想等待所有 4 个字符串都排在前面,然后再执行操作。

这四个字符串每次都以不同的顺序出现。感谢您的帮助。

【问题讨论】:

    标签: python telnetlib


    【解决方案1】:

    您可以使用the .expect method等待abcd

    Telnet.期望(列表[, 超时])

    读取直到匹配正则表达式列表中的一个。

    所以:

    (index, match, content_including_abcd) = tn.expect(['a', 'b', 'c', 'd'], timeout)
    

    达到超时时返回(-1, None, current_buffer)


    我们可以轻松地将其更改为循环以等待abc 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]
    

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 2011-03-26
      • 2018-09-14
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      相关资源
      最近更新 更多