出错客户端代码:

1
2
3
with pysftp.Connection(host, port, username, password, cnopts) as sftp:
# upload a 10M size file to sftp server
return True

return True这一行抛错:

1
2
3
4
5
6
7
8
9
10
11
12
File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__ self.gen.throw(type, value, traceback)
File "/usr/local/lib/python2.7/dist-packages/pysftp/__init__.py", line 511, in cd self.cwd(original_path)
File "/usr/local/lib/python2.7/dist-packages/pysftp/__init__.py", line 524, in chdir self._sftp.chdir(remotepath)
File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 659, in chdir if not stat.S_ISDIR(self.stat(path).st_mode):
File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 493, in stat t, msg = self._request(CMD_STAT, path)
File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 812, in _request num = self._async_request(type(None), t, *arg)
File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp_client.py", line 837, in _async_request self._send_packet(t, msg)
File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp.py", line 198, in _send_packet self._write_all(out)
File "/usr/local/lib/python2.7/dist-packages/paramiko/sftp.py", line 162, in _write_all n = self.sock.send(out)
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 801, in send return self._send(s, m)
File "/usr/local/lib/python2.7/dist-packages/paramiko/channel.py", line 1180, in _send raise socket.error("Socket is closed")
error: Socket is closed

找到这几个有用的链接

  1. https://www.cnblogs.com/Vooom/p/5820644.html
    这篇说的是使用paramiko连接ssh时,可以通过ssh_client.invoke_shell()来keep alive
  2. https://gaomf.cn/2017/01/10/SSH_Broken_Pipe/
    这篇说的是ssh连接可以分别通过客户端和服务端两个参数的配置实现连接keep alive
  3. http://docs.paramiko.org/en/latest/api/transport.html#paramiko.transport.Transport.set_keepalive
    这是paramiko的官方文档中提到的客户端设置keepalive的配置
  4. https://programtalk.com/python-examples/pysftp.Connection/
    这篇提到pysftp可以通过sftp_connection._transport获得对应的paramiko连接,然后通过set_keepalive来设置