pexpect.exceptions.TIMEOUT: Timeout exceeded.

本文介绍了一个Python项目中使用pexpect模块连接远程FTP服务器时遇到的超时异常问题及解决方案。通过修改expect()函数的匹配条件,解决了因直接连接成功导致的异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 摘要:在一个Python项目中,要执行“/usr/bin/sftpaliyunzixun@xxx.com”命令连接远程FTP服务器,代码如下:_pass='assword:'_prompt='sftp>'_newKey="Areyousureyouwanttocontinueconnecting(yes/no)?"self._client=pexpect.spawn(command_connect_sftp)match=[self._pass,self._
  • 在一个Python项目中,要执行“/usr/bin/sftp aliyunzixun@xxx.com”命令连接远程FTP服务器,代码如下:

     

    _pass = 'assword:'_prompt = 'sftp> '_newKey = "Are you sure you want to continue connecting (yes/no)?"self._client = pexpect.spawn(command_connect_sftp)match = [self._pass, self._newKey]i = self._client.expect(match)if i == 1: self._client.sendline("yes") self._client.expect(match)self._client.sendline(password)self._client.expect(self._prompt) 
    可以看到,在发生连接FTP服务器的命令后,通过expect()检查可能的结果。但是在执行过程中得到如下异常:

     

     

    ERROR Internal system error occured handling command install for lidus2376 help Internal system error! Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/pexpect/expect.py", line 99, in expect_loop incoming = spawn.read_nonblocking(spawn.maxread, timeout) File "/usr/local/lib/python3.5/dist-packages/pexpect/pty_spawn.py", line 462, in read_nonblocking raise TIMEOUT('Timeout exceeded.') pexpect.exceptions.TIMEOUT: Timeout exceeded. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/userbbauto/bbauto/src/bbautoserver.py", line 335, in _handle_api_install_request tgf_install = TgfInstall(self.input_params) File "/home/userbbauto/bbauto/src/tgfinstall.py", line 52, in __init__ debug=self.active_debug_groups, password=self.BBAUTO_PASSWORD) File "/home/userbbauto/bbauto/src/sftpconnection3.py", line 47, in __init__ i = self._client.expect(match) File "/usr/local/lib/python3.5/dist-packages/pexpect/spawnbase.py", line 321, in expect timeout, searchwindowsize, async) File "/usr/local/lib/python3.5/dist-packages/pexpect/spawnbase.py", line 345, in expect_list return exp.expect_loop(timeout) File "/usr/local/lib/python3.5/dist-packages/pexpect/expect.py", line 107, in expect_loop return self.timeout(e) File "/usr/local/lib/python3.5/dist-packages/pexpect/expect.py", line 70, in timeout raise TIMEOUT(msg) pexpect.exceptions.TIMEOUT: Timeout exceeded. command: /usr/bin/sftp args: ['/usr/bin/sftp', 'aliyunzixun@xxx.com'] buffer (last 100 chars): b' NOT AN AUTHORIZED USER, PLEASE EXIT IMMEDIATELY/r/nConnected to ftp.ericsson.se./r/nsftp> ' before (last 100 chars): b' NOT AN AUTHORIZED USER, PLEASE EXIT IMMEDIATELY/r/nConnected to ftp.ericsson.se./r/nsftp> ' after: match: None match_index: None exitstatus: None flag_eof: False pid: 73 child_fd: 9 closed: False timeout: 30 delimiter: logfile: None logfile_read: None logfile_send: None maxread: 2000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1 searcher: searcher_re: 0: re.compile("b'assword:'") 1: re.compile("b'Are you sure you want to continue connecting (yes/no)?'") 

    分析发现异常的原因是expect()语句超时,期望得到的结果没有得到,直到超时抛出异常。

    这是因为期望得到的结果只包含两种情况,一种是提示将FTP服务器加入到known_hosts中,另一种是提示输入密码。但是,由于当前机器上不检查hosts的公钥(避免了第一种提示),使用提前生成的私钥(避免了第二章提示),因而还有第三种可能,就是使用用户名和私钥直接连接FTP成功,从而得到了“ftp> ”的结果。

    修改代码如下,解决该异常:

     

    ...match = [self._pass, self._newKey, self._promt]i = self._client.expect(match)if i==2: returnif i==1:... 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值