Python self-developed FTP programming module ftplib
Directly paste several lines of code for analysis
import os, sysfrom ftplib import FTPftpsite = "ftp.site"userinfo = ('aaron', getpass('123456'))print('Connecting')connection = ftplib.FTP(sitename)connnection.login(*userinfo) ##() for anonymous loginconnection.nlst() ##nlst() gives files list ##dir() gives full details) connection.cwd(".") ##cd to directoryfilename = 'filename'##########download a file#############localfile = open(filename, 'wb') ##local file to store downloadconnection.retrbinary('RETR ' + filename, localfile.write, 1024)localfile.close()##########download a file#######################upload a file#############localfiletoupload = open(filename, 'rb')connection.storbinary('STOR ' + filename, localfiletoupload, 1024)##########upload a file#############connection.quit()
The above Code has not been tested!
It mainly lists several common operations:
Log on to the FTP site:
connection = ftplib.FTP(sitename)connnection.login(*userinfo) ##() for anonymous login
NLST () # obtain the file list
Retrbinary () # download an object
Storbinary () # upload a file
CWD ('dir') # Switch to the directory
Only list these items. For other operations, see help ()
I recommend a python book: Python programming (this book does not talk about the basic Python syntax and has a wide range of application scenarios. It is recommended that you have a bit of Python basics)
connection = ftplib.FTP(sitename)connnection.login(*userinfo) ##() for anonymous login