Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 2.7中使用telnet.expect的问题_Python - Fatal编程技术网

在python 2.7中使用telnet.expect的问题

在python 2.7中使用telnet.expect的问题,python,Python,我正在尝试telnet到远程服务器,并尝试获取响应 我在前面使用了telnet.read_,直到匹配是否出现提示/模式,但是read_,直到返回所有内容,即使没有匹配。我曾想过使用telnet.expect,但我遇到了一个错误 下面是代码 com = re.compile("\#") # is the prompt tn.write("somecommand" + "\n") res = tn.expect(com, 10) 我得到的错误是 File "reg.txt", line 23,

我正在尝试telnet到远程服务器,并尝试获取响应

我在前面使用了
telnet.read_,直到
匹配是否出现提示/模式,但是read_,直到返回所有内容,即使没有匹配。我曾想过使用
telnet.expect
,但我遇到了一个错误

下面是代码

com = re.compile("\#") # is the prompt
tn.write("somecommand" + "\n")
res = tn.expect(com, 10)
我得到的错误是

  File "reg.txt", line 23, in login
    res = tn.expect(com, 10)
  File "C:\Python27\lib\telnetlib.py", line 593, in expect
    list = list[:]
TypeError: '_sre.SRE_Pattern' object is not subscriptable
telnetlib.expect()

发件人:

expect(列表,超时=无)

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

第一个参数是已编译(regex对象)或未编译(字节字符串)的正则表达式列表。可选的第二个参数是超时,以秒为单位;默认设置为无限期阻止

[……]


应该有效(差异是
expect([com])
,而不是
expect(com)

com = re.compile("\#") # is the prompt
tn.write("somecommand" + "\n")
res = tn.expect([com], 10)