【问题标题】:Send file with sftp over ssh.client in Python error在 Python 错误中通过 ssh.client 使用 sftp 发送文件
【发布时间】:2015-10-18 04:11:40
【问题描述】:

我正在尝试使用 Paramiko 和 SFTP 通过 SSH 发送文件。上传成功完成,但文件似乎不完整。可能是什么原因?

path = 'data.json'
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(host, username=user, password=pass)
sftp = ssh.open_sftp()
sftp.put(path,path)
sftp.close()
ssh.close()

DEB [20150727-21:37:41.332] thr=1   paramiko.transport: starting thread (client mode): 0xa212ad0L
INF [20150727-21:37:41.355] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_6.2)
DEB [20150727-21:37:41.360] thr=1   paramiko.transport: kex algos:[u'some_group'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'some_username'] server encrypt:[u'some_username'] client mac:[u'some_user'] server mac:[u'some_mac'] client compress:[u'none'] server compress:[u'none'] client lang:[u''] server lang:[u''] kex follows?False
DEB [20150727-21:37:41.360] thr=1   paramiko.transport: Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEB [20150727-21:37:41.360] thr=1   paramiko.transport: using kex diffie-hellman-group14-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
DEB [20150727-21:37:41.401] thr=1   paramiko.transport: Switch to new keys ...
DEB [20150727-21:37:41.409] thr=1   paramiko.transport: userauth is OK
DEB [20150727-21:37:41.421] thr=1   paramiko.transport: Authentication type (password) not permitted.
DEB [20150727-21:37:41.421] thr=1   paramiko.transport: Allowed methods: [u'publickey', u'keyboard-interactive']
DEB [20150727-21:37:41.426] thr=1   paramiko.transport: userauth is OK
INF [20150727-21:37:41.553] thr=1   paramiko.transport: Authentication (keyboard-interactive) successful!
DEB [20150727-21:37:41.562] thr=2   paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20150727-21:37:41.570] thr=1   paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20150727-21:37:41.570] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20150727-21:37:41.571] thr=1   paramiko.transport: [chan 0] Sesch channel 0 request ok
INF [20150727-21:37:41.581] thr=2   paramiko.transport.sftp: [chan 0] Opened sftp connection (server version 3)
DEB [20150727-21:37:41.581] thr=2   paramiko.transport.sftp: [chan 0] open('/Users/arturataide/Desktop/data.json', 'wb')
DEB [20150727-21:37:41.582] thr=2   paramiko.transport.sftp: [chan 0] open('/Users/arturataide/Desktop/data.json', 'wb') -> 00000000
DEB [20150727-21:37:41.583] thr=2   paramiko.transport.sftp: [chan 0] close(00000000)
DEB [20150727-21:37:41.583] thr=2   paramiko.transport.sftp: [chan 0] stat('/Users/arturataide/Desktop/data.json')
DEB [20150727-21:37:41.584] thr=2   paramiko.transport.sftp: [chan 0] stat('/Users/arturataide/Desktop/data.json')
INF [20150727-21:37:41.584] thr=2   paramiko.transport.sftp: [chan 0] sftp session closed.
DEB [20150727-21:37:41.584] thr=2   paramiko.transport: [chan 0] EOF sent (0)
DEB [20150727-21:37:41.585] thr=1   paramiko.transport: EOF in transport thread

我在 Django 中这样做。

我需要一些帮助。谢谢

编辑:

我可以成功上传图片文件,但是json文件仍然没有完全上传

【问题讨论】:

  • 您是否有显示文件传输方式的堆栈跟踪,以及传输失败的可能位置?堆栈跟踪(或回溯)是一种很好的调试方式。
  • @HunterStevens 谢谢。我已经完成了,文件传输似乎没有错误完成。
  • @HunterStevens 我已经上传了日志文件
  • 当您说“已解决”时,您的意思是您的问题已得到解答吗?如果您回答了自己的问题,请通过将其标记为完成来提供帮助 --- 提交答案,详细解释您的进度以及您的发现/结论。然后,如果这是您的最终答案,请点击复选标记使其成为“接受的答案”。最后,从您的问题中删除“已解决”部分。

标签: python django ssh sftp


【解决方案1】:

如果没有看到回溯,我会假设错误存在于这一行中。

sftp.put(path,path )sftp.close()

请尝试以下方法:

sftp.put(path,path )
sftp.close()

【讨论】:

  • 这是一个错字。我已经通过这种方式进行了回溯:try: sendFile() except Exception, err: print(traceback.format_exc())
  • 划掉最后一条评论。
  • @SteveO'Driscoll 已将日志添加到问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 2012-03-02
  • 1970-01-01
相关资源
最近更新 更多