# pip install pysftp
import pysftp


class SftpUtil(object):
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password

def get_connnet(self):
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# sftp = pysftp.Connection(host='192.168.10.51', port=2222, username='xx', password='xx', cnopts=cnopts)
sftp = pysftp.Connection(host=self.host, port=self.port, username=self.username, password=self.password,
cnopts=cnopts)
return sftp

'''./xx.pptx upload/seatest/ XX.PPTX '''

def upload(self, local_file_path, remote_file_dir, remote_file_name):
sftp = self.get_connnet()
result = None
if sftp.exists(remote_file_dir):
result = sftp.put(local_file_path, remote_file_dir + remote_file_name)
else:
sftp.mkdir(remote_file_dir)
result = sftp.put(local_file_path, remote_file_dir + remote_file_name)
sftp.close
print("upload " + local_file_path+" over")
print(str(result))
return result

if __name__ == '__main__':
# cnopts = pysftp.CnOpts()
# cnopts.hostkeys = None
# sftp = pysftp.Connection(host='192.168.18.51', port=2222, username='root', password='root', cnopts=cnopts)
# if sftp.exists("upload/seatest"):
# sftp.put('./xx.pptx', 'upload/seatest/xx.pptx')
# else:
# sftp.mkdir("upload/seatest")
# sftp.put('./xx.pptx', 'upload/seatest/xx.pptx')
# print("over")
# sftp.close()
util = SftpUtil(host="192.168.18.51", port=2222, username="root", password="root")
util.upload("./swift.pptx", "upload/seatest/", "xx.ptx")
posted on 2023-08-10 13:18  lshan  阅读(69)  评论(0编辑  收藏  举报