Copy the code

You need to upload and download various files under THE FTP protocol, so learn about the Ftplib module in Python. The ftplib module, installed by default in Python, defines the FTP class, which can be used to implement a simple FTP client for uploading or downloading files.

First hit the bottom: to understand the main, not comprehensive, if read or not, that must be my writing is not good enough.

1. FTP login connection

# file server parameter ftp_param = {' host ':' 10.12.8.22 ', 'the port: 2121,' user ':' user ', 'the PWD' : 'uesr1234', 'points_dir' : 'comm/cust_point/',' the XSL: 'the XSL/DPS'} ftp_param = {' host ':' 10.12.8.22 ', 'the port: 2121,' user ':' user ', 'the PWD' : 'uesr1234', 'points_dir': 'comm/cust_point/', 'xsl': 'xsl/dps' }Copy the code
From ftplib import FTPimport logging.configLogger = logging.getLogger(__name__)# Set the variable FTP =FTP() # connect to FTP Ftp. connect(ftp_param['host'], ftp_param['port'])# ftp.login(ftp_param['user'], Logger.debug (ftp.getwelcome())# Enter the remote directory ftp.cwd(ftp_param[' XSL '])# File filename="xsl.txt"# File_handle =open(filename,"wb"). Write# Accept file on server and write to local file ftp.retrbinaly("RETR xsl.txt",file_handle,bufsize) # Quit () from ftplib import FTP import logging.config logger = logging.getLogger(__name__) # Set variable FTP =FTP() Ftp. connect(ftp_param['host'], ftp_param['port']) Logger.debug (FTp.getwelcome ()) # Enter the remote directory FTp.cwd (ftp_param[' XSL ']) # The file to download File_handle =open(filename,"wb"). Write # Receive the file on the server and write to the local file ftp.retrbinaly("RETR ") TXT ",file_handle,bufsize) # quit()Copy the code

2. Run FTP commands

ftp.cwd(pathname).cwd(pathname)
Copy the code
ftp.dir().dir()
Copy the code
ftp.nlst().nlst()
Copy the code
ftp.mkd(pathname) .mkd(pathname) 
Copy the code
ftp.pwd().pwd()
Copy the code
ftp.rmd(dirname).rmd(dirname)
Copy the code
ftp.delete(filename)
Copy the code
ftp.rename(fromname, toname).rename(fromname, toname)
Copy the code
ftp.storbinaly("STOR filename.txt",file_handel,bufsize).storbinaly("STOR filename.txt",file_handel,bufsize)
Copy the code
ftp.retrbinary("RETR filename.txt",file_handel,bufsize).retrbinary("RETR filename.txt",file_handel,bufsize)
Copy the code

Note:

Example 3.

Code has been uploaded to [Github] address: github.com/ReainL/Ftpl…

Hot recommended in Python create WeChat robot in Python robot to monitor WeChat group chat in Python for cameras and real-time control face open source project | beautification LeetCode warehouse is recommended in Python Python Chinese community’s several kind of public service, announced notice | Python Chinese Community award for articles \