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模块
- Ftp=ftp () #设置变量
- Ftp.set_debuglevel (2) #打开调试级别2, show details
- Ftp.connect ("IP", "Port") #连接的ftp Sever and ports
- Ftp.login ("User", "password") #连接的用户名, password
- Print Ftp.getwelcome () #打印出欢迎信息
- Ftp.cmd ("xxx/xxx") #更改远程目录
- bufsize=1024 #设置的缓冲区大小
- Filename= "Filename.txt" #需要下载的文件
- File_handle=open (filename, "WB"). Write #以写模式在本地打开文件
- Ftp.retrbinaly ("RETR filename.txt", file_handle,bufsize) #接收服务器上文件并写入本地文件
- Ftp.set_debuglevel (0) #关闭调试模式
- Ftp.quit #退出ftp
FTP-related command operations
- FTP.CWD (pathname) #设置FTP当前操作的路径
- Ftp.dir () #显示目录下文件信息
- Ftp.nlst () #获取目录下的文件
- FTP.MKD (pathname) #新建远程目录
- Ftp.pwd () #返回当前所在位置
- FTP.RMD (dirname) #删除远程目录
- Ftp.delete (filename) #删除远程文件
- Ftp.rename (FromName, ToName) #将fromname修改名称为toname.
- Ftp.storbinaly ("STOR filename.txt", file_handel,bufsize) #上传目标文件
- 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