【问题标题】:pexpect : TypeError: "unsupported operand type(s) for +: 'float' and 'str'"pexpect:TypeError:“+ 不支持的操作数类型:‘float’和‘str’”
【发布时间】:2016-09-03 11:48:38
【问题描述】:

我对python不太熟悉。在“pexpect”示例回购中找到了这段代码。我已经进行了必要的更改。但是,它正在抛出“TypeError”。无法弄清楚为什么。任何人都可以解释为什么以及如何解决这个问题?

“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)

【问题讨论】:

    标签: python-2.7 ssh pexpect


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      相关资源
      最近更新 更多