在 Python 错误中通过 ssh.client 使用 sftp 发送文件

Send file with sftp over ssh.client in Python error

我正在尝试使用 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文件还是没有上传完整

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

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

请尝试以下操作:

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