#-*-coding:utf-8-*-#python#Xiaodeng#Python module ftplib (Client of FTP protocol)#Requirements: Fast ftp upload, download, query files fromFtplibImportftpftp= FTP ()#Setting VariablesTimeout= 30Port= 21Ftp.connect ('192.168.1.188', Port,timeout)#connecting to an FTP serverFtp.login ('UserName','888888')#LoginPrintFtp.getwelcome ()#Get Welcome InfoFTP.CWD ('file/test')#Set FTP remote directory (PATH)List = Ftp.nlst ()#get the file in the directory, get the directory list forNameinchList:printnamepath='d:/data/'+ Name#define file Save pathf = open (path,'WB')#Open the file you want to savefilename ='RETR'+ Name#Save ftp fileFtp.retrbinary (Filename,f.write)#save files on FTPFtp.delete (name)#Delete ftp fileFtp.storbinary ('STOR'+filename, open (path,'RB'))#Uploading FTP filesftp.quit ()" " Example: >>> from ftplib import FTP >>> FTP = FTP (' ftp.python.org ') #连接ftp服务器; connect to host, Default port >>> ftp.login () # Default, i.e.: User anonymous, passwd [email protected] ' Guest login OK, access restrictions apply. ' >>> ftp.retrlines (' list ') # List Directory contents Total 9 Drwxr-xr-x 8 root wheel 1024 Jan 3 1994. Drwxr-xr-x 8 Root Wheel 1024x768 3 1994.. Drwxr-xr-x 2 Root Wheel 1024x768 3 1994 bin Drwxr-xr-x 2 root wheel 1024x768 3 1994 etc D-wxrwxr-x 2 FTP Wheel SEP 5 13:43 incoming drwxr-xr-x 2 root wheel 1024x768 17 199 3 Lib Drwxr-xr-x 6 1094 wheel SEP 19:07 pub drwxr-xr-x 3 root wheel 1024x768 3 1 994 usr-rw-r--r--1 root root 312 1 1994 welcome.msg ' 226 Transfer complete. ' >>> Ftp.quit () #断开服务器连接 ' 221 Goodbye. ' Class FTP ftp=ftp () #设置变量, similar to initialization | An FTP client class. | Methods: | Acct (Self, password) | Send new account name. | | Close (self) #close连接 | Close the connection without assuming anything about it. | | Connect (self, host= ", port=0, timeout=-999) #连接的ftp Sever and ports, such as: Ftp.connect (' 192.168.1.188 ', 21,30) | | CWD (self, dirname) #把当前目录设置为path, sets the path of the FTP current operation | Change to a directory. | | debug = Set_debuglevel (self, level) #ftp. Set_debuglevel (2) #打开调试级别2, show details | | Delete (self, filename) #删除远程文件 | Delete a file. | | Dir (self, *args) #显示目录下文件信息 | List a directory in long form. | By default list current directory to stdout. | Optional last argument is callback function; All | Non-empty arguments before it is concatenated to the | LIST command. (This *should* is used for a pathname.) | | Getline (self) #从服务器输出一行数据 | # Internal:return one line from the server, stripping CRLF. | # Raise Eoferror If the connection is closed | | Getmultiline (self) | # Internal:get A response from the server, which may possibly | # consist of multiple lines. Return a single string with no | # trailing CRLF. If The response consists of multiple lines, | # These is separated by ' \ n ' characters in the string | | Getresp (self) | # Internal:get A response from the server. | # Raise Various Errors If the response indicates an error | | Getwelcome (self) #打印出欢迎信息 | Get the welcome message from the server. | (This was read and squirreled away by Connect ()) | | Login (self, user= ', passwd= ', acct= ') #登录到FTP服务器, all parameters are optional. | Login, default anonymous. | | MAKEPASV (self) | | Makeport (self) #创建一个新的套接字 and sends a port command. Create a new socket and send a PORT command for it. | | MKD (self, dirname) #创建远程目录; Create a directory that returns its full pathname | Make a directory, return it full pathname. | | Nlst (self, *args) is similar #与dir (), but returns a list of file names instead of displaying them #返回给定目录下的文件列表 (by default) | Return a list of files in a given directory (default the current). | | Ntransfercmd (self, cmd, rest=none) | Initiate a transfer over the data connection. | | If the transfer is active, send a port command and the | Transfer command, and accept the connection. If the server is | PassIve, send a PASV command, connect to it, and start the | Transfer command. Either, return the socket for the | Connection and the expected size of the transfer. The | Expected size May is None if it could not being determined. | | Optional ' rest ' argument can be a string this is sent as the | argument to a REST command. This is essentially a server | Marker used to tell the server to skip over any data up to the | Given marker. | | Putcmd (self, line) | # internal:send One command to the server (through Putline ()) | | Putline (self, line) | # Internal:send one line to the server, appending CRLF | | PWD (self) #当前工作目录 | Return to current working directory. | | Quit (self) #退出ftp | Quit, and close the connection. | | Rename (self, fromname, ToName) #Change the filename to rename the remote file FromName to ToName | Rename a file. | | Retrbinary (self, cmd, callback, blocksize=8192, Rest=none) #与retrlines () Similar, just this instruction handles binary files. callback Function | Retrieve data in binary mode. A New Port is created. | | Args: | Cmd:a RETR command. | Callback:a single parameter callable to being called on each | Block of data read. | Blocksize:the maximum number of bytes to read from the | Socket at one time. [default:8192] | Rest:passed to Transfercmd (). [Default:none] | | Returns: | The response code. | | Retrlines (self,cmd,callback=none) # #ftp. Retrlines (' LIST ') #返回目录内容 #此时可以获得当前ftp目录下的所有文件的信息 | Retrieve data in line mode. A New Port is created for YOu. | | Args: | Cmd:a RETR, LIST, NLST, or MLSD command. | Callback:an Optional Single parameter callable it is called | With the trailing CRLF stripped. | [Default:print_line ()] | | Returns: | The response code. | | RMD (self, dirname) #删除远程目录 | Remove a directory. | | Sanitize (self, s) | # Internal: "sanitize" a String for printing | | Sendcmd (self, cmd) | Send a command and return the response. | | Sendeprt (self, host, port) | Send a EPRT command with the current host and the given port number. | | SendPort (self, host, port) | Send a PORT command with the current host and the given | Port number. | | Set_debuglevel (self, level) #ftp. Set_debuglevel (2) #打开调试级别2, show details #ftp. Set_debuglevel (0) #关闭调试模式 | Set the debugging level. | The required argument level means: | 0:no debugging output (default) | 1:print commands and responses but not body text etc. | 2:also print raw lines read and sent before stripping Cr/lf | | SET_PASV (Self, val) | Use passive or active mode for data transfers. | With a false argument, use the normal PORT mode, | With a true argument, use the PASV command. | | Size (self, filename) #检索文件大小 | Retrieve the size of a file. | | Storbinary (self, cmd, FP, blocksize=8192, Callback=none, Rest=none) #上传FTP文件 #ftp. Storbinaly ("STOR filename.txt", file_handel,bufsize) #上传目标文件 #ftp. S Torbinary (' STOR ' +filename, open (path, ' RB ')) # Upload FTP file #注意storlines的解释 #只是这个指令处理二进制文件. To give a file object F, upload block size BS default to 8kbs=8192]) | Store a file in binary mode. A New Port is created. | | Args: | Cmd:a STOR command. | Fp:a File-like object with A Read (Num_bytes) method. | Blocksize:the maximum data size to read from FP and send over | The connection at once. [default:8192] | Callback:an Optional Single parameter callable that's called on | Each of the block of data after it is sent. [Default:none] | Rest:passed to Transfercmd (). [Default:none] | | Returns: | The response code. | | Storlines (self, cmd, FP, Callback=none) #storlines (cmd, f) #给定FTP command (such as "STOR filename") to upload a text file. To be given a file object F | Store a file in line mode. A New Port is created. | | Args: | Cmd:a STOR command. | Fp:a File-like object with A ReadLine () method. | Callback:an Optional Single parameter callable that's called on | Each of the it is sent. [Default:none] | | Returns: | The response code. | | Transfercmd (self, cmd, rest=none) | Like Ntransfercmd () but returns only the socket. | | Voidcmd (self, cmd) | Send a command and expect a response beginning with ' 2 '. | | Voidresp (self) | Expect a response beginning with ' 2 '. DATA __all__ = [' FTP ', ' Netrc ', ' Ftp_tls ']" "
Python module ftplib (Client of FTP protocol)