擅长:python、mysql、java
<p>改为使用paramiko的SFTP客户端。这个示例程序在复制之前检查是否存在。</p>
<pre><code>#!/usr/bin/env python
import paramiko
import getpass
# make a local test file
open('deleteme.txt', 'w').write('you really should delete this]n')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect('localhost', username=getpass.getuser(),
password=getpass.getpass('password: '))
sftp = ssh.open_sftp()
sftp.chdir("/tmp/")
try:
print(sftp.stat('/tmp/deleteme.txt'))
print('file exists')
except IOError:
print('copying file')
sftp.put('deleteme.txt', '/tmp/deleteme.txt')
ssh.close()
except paramiko.SSHException:
print("Connection Error")
</code></pre>