pexpect : TypeError: "unsupported operand type(s) for +: 'float' and 'str'"

pexpect : TypeError: "unsupported operand type(s) for +: 'float' and 'str'"

我对python不是很熟悉。在 "pexpect" 示例存储库中找到了这段代码。我已经做了必要的改变。但是,它正在抛出 "TypeError"。无法弄清楚 why.anybody 可以解释为什么以及如何解决这个问题?

"pdb" 在 "child = ssh_command(user, host, password)"

上发出错误

pdb 错误:

TypeError: "unsupported operand type(s) for +: 'float' and 'str'"

代码如下,

import pexpect

def ssh_command(user, host, password):
    new_sshkey = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh %s@%s' %(user, host))
    i = child.expect(new_sshkey, 'password: ')
    if i == 0:
        print 'ERROR.! SSH could not login.'
        print child.before, child.after
    if i == 1:
        child.sendline('yes')
        child.expect('password: ')
        i = child.expect('password: ')
        if i == 0:
            print 'ERROR.! Permission Denied'
            print child.before, child.after
    return child

def main():
    host = raw_input('Hostname: ')
    user = raw_input('Username: ')
    password = raw_input('Password: ')
    child = ssh_command(user, host, password)
    child.expect(pexpect.EOF)
    print child.before

if __name__ == '__main__':
    try:
        main()
    except Exception, e:
        print str(e)

很可能错误是由这一行引起的

i = child.expect(new_sshkey, 'password: ')

根据文档,expect 方法声明如下

child.expect(pattern, timeout=-1, searchwindowsize=-1, async=False)

在您的情况下,您将 'password: ' 作为第二个参数 (timeout) 传递 可能应该是 int.