PYTHON3_FTP File Transfer

Source: Internet
Author: User
Tags ftp login ftp client

The Ftplib module in Python

The Ftplib module, which is installed by default in Python, defines the FTP class, where functions are limited and can be used to implement a simple FTP client for uploading or downloading files

FTP Workflow and basic operation can refer to protocol RFC959

FTP Login Connection

From ftplib import FTP #加载ftp模块

    1. Ftp=ftp () #设置变量
    2. Ftp.set_debuglevel (2) #打开调试级别2, show details
    3. Ftp.connect ("IP", "Port") #连接的ftp Sever and ports
    4. Ftp.login ("User", "password") #连接的用户名, password
    5. Print Ftp.getwelcome () #打印出欢迎信息
    6. Ftp.cmd ("xxx/xxx") #更改远程目录
    7. bufsize=1024 #设置的缓冲区大小
    8. Filename= "Filename.txt" #需要下载的文件
    9. File_handle=open (filename, "WB"). Write #以写模式在本地打开文件
    10. Ftp.retrbinaly ("RETR filename.txt", file_handle,bufsize) #接收服务器上文件并写入本地文件
    11. Ftp.set_debuglevel (0) #关闭调试模式
    12. Ftp.quit #退出ftp

FTP-related command operations

    1. FTP.CWD (pathname) #设置FTP当前操作的路径
    2. Ftp.dir () #显示目录下文件信息
    3. Ftp.nlst () #获取目录下的文件
    4. FTP.MKD (pathname) #新建远程目录
    5. Ftp.pwd () #返回当前所在位置
    6. FTP.RMD (dirname) #删除远程目录
    7. Ftp.delete (filename) #删除远程文件
    8. Ftp.rename (FromName, ToName) #将fromname修改名称为toname.
    9. Ftp.storbinaly ("STOR filename.txt", file_handel,bufsize) #上传目标文件
    10. Ftp.retrbinary ("RETR filename.txt", file_handel,bufsize) #下载FTP文件
1  fromFtplibImportFTP2 3 4 defFtpconnect (host, username, password):5FTP =FTP ()6Ftp.set_debuglevel (2)7Ftp.connect (host, 21)8 ftp.login (username, password)9     returnFTPTen  One  A defdownloadfile (FTP, RemotePath, localpath): -     #download files from FTP -BufSize = 1024 thefp = open (LocalPath,'WB') -Ftp.retrbinary ('RETR'+RemotePath, Fp.write, bufsize) - ftp.set_debuglevel (0) - fp.close () +  -  + defUploadFile (FTP, LocalPath, RemotePath): A     #upload files from local to FTP atBufSize = 1024 -fp = open (LocalPath,'RB') -Ftp.storbinary ('STOR'+RemotePath, FP, bufsize) - ftp.set_debuglevel (0) - fp.close () -  in if __name__=="__main__": -FTP = Ftpconnect ("10.1.2.254","Download","Download") toUploadFile (FTP,"/home/rxf/other/platservertest/2.jpg","d:/download/ranxiangfei/2.jpg") +DownloadFile (FTP,"d:/download/ranxiangfei/svn/v2.0.xxx.171030_alpha/xzrs.tar.gz", -                  "/home/rxf/svn/v2.0.xxx.171030_alpha/xzrs.tar.gz") the  *Ftp.quit ()

      

Python3_ftp File Transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.