Python Telnet 如果读取 = xxxx

Python Telnet if read = xxxx

我在 Python 中遇到了 Telnet 的小问题。我试图通过确定 telnet 连接是否已登录来启动脚本。我的代码如下所示:

if TLNT.read_until("login: ",2): #if it reads the login prompt:
    login(USER,PASS) #runs the function used to login to the system
main() #otherwise will determine system is already logged in and start the main script

这里的问题我不确定这里的格式,因为脚本将跳过登录功能,即使我故意注销了 telnet 系统,所以我知道它会看到登录提示。如果 telnet 提示留在 "login: " 会怎样 read_until return?

答案在documentation!

Telnet.read_until(expected[, timeout])

Read until a given string, expected, is encountered or until timeout seconds have passed.

When no match is found, return whatever is available instead, possibly the empty string. Raise EOFError if the connection is closed and no cooked data is available.

它没有 return TrueFalse,因此您可能不想使用 return 值作为条件表达式。