# pip install pysftp
import pysftp
class SftpUtil(object):
def __int__(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
def upload(self, local_file_path, remote_file_dir, remote_file_name):
sftp = self.get_connnet()
if sftp.exists(remote_file_dir):
return sftp.put(local_file_path, remote_file_dir + remote_file_name)
else:
sftp.mkdir(remote_file_dir)
return sftp.put(local_file_path, remote_file_dir + remote_file_name)
sftp.close
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()