python3操作ftp

ftp相关的包不需要安装,python自带

# -*- coding:utf-8 -*-

from ftplib import FTP

def upload(f, remote_path, local_path):
    fp = open(local_path, "rb")
    buf_size = 1024
    f.storbinary("STOR {}".format(remote_path), fp, buf_size)
    fp.close()


def download(f, remote_path, local_path):
    fp = open(local_path, "wb")
    buf_size = 1024
    f.retrbinary('RETR {}'.format(remote_path), fp.write, buf_size)
    fp.close()


if __name__ == "__main__":
    ftp = FTP()
    ftp.connect("172.31.160.51", 21)
    ftp.login("用户名", "密码")
    ftp.cwd("download/20201223ftp") #切换到ftp的某个目录下
    upload(ftp, "code_1.js", "code.js") #将本地code.js文件上传到ftp,并且重命名为code_1.js
    download(ftp, "code_1.js", "code_2.js") #将ftp的code_1.js下载的本地,命名为code_2.js
    ftp.quit()

 

posted @ 2020-12-23 17:10  lingwang3  阅读(272)  评论(0编辑  收藏  举报