随笔 - 836  文章 - 1 评论 - 40 阅读 - 102万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

 

# 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   lshan  阅读(79)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示