python3 sftp上传使用的是paramiko模块
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191127150629639.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zMDk5OTg0Mw==,size_16,color_FFFFFF,t_70)
import paramiko
import os
def sftp_upload(host,port,username,password,local,remote):
sf = paramiko.Transport((host,port))
sf.connect(username = username,password = password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local):
for f in os.listdir(local):
sftp.put(os.path.join(local + f), os.path.join(remote + f))
else:
sftp.put(local, remote)
except Exception :
print('upload error:')
sf.close()
def sftp_download(host,port,username,password,local,remote):
sf = paramiko.Transport((host,port))
sf.connect(username = username,password = password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local):
for f in sftp.listdir(remote):
sftp.get(os.path.join(remote+f),os.path.join(local+f))
else:
sftp.get(remote,local)
except Exception:
print('download exception:')
sf.close()
if __name__ == '__main__':
host = ''
port = 22
username = ''
password = ''
upload_local = 'test.py'
upload_remote = '/xinjiang_photo/test.py'
sftp_upload(host,port,username,password,upload_local,upload_remote)
down_local = 'T:\pythonxuexi\staff下载\zip\\111.zip'
down_remote = '/xinjiang_photo/aaa.zip'
sftp_download(host,port,username,password,down_local,down_remote)