ftplib implements FTP upload and download functions

The default port number of the FTP server is 21.

The reference address of this article: https://docs.python.org/3.6/library/ftplib.html

1. ftplib contains FTP andFTP_TLS两个class,后者是封装了TLS安全传输协议的FTP,本文不多描述,查看官网即可,很简单。

2. The ftplib.FTP class is a class that simulates the FTP protocol client. You can use this class to perform various interactions with the FTP server, such as data upload and download.

3. FTP data transmission has two modes ASCII and binary format, generally speaking, the use of binary format is more common, more applicable scenarios. Therefore, the four most commonly used methods of the FTP class are actually:

  • FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)
  • FTP.storlines(cmd, fp, callback=None)
  • FTP.retrbinary(cmd, callback, blocksize=8192, rest=None)
  • FTP.retrlines(cmd, callback=None)

Among them, storbinary and retrbinary are methods for binary data transmission.

FTP数据下载:

一般来说,生产上会将ftp用户的访问权限限定在自己的家目录下(可通过修改/etc/vsftpd.conf来改变此行为模式)。

# The FTP class supports the with statement, eg: 
from ftplib import FTP 
with FTP ('<ftp server IP>', 'user', 'passwd') as c, \ 
	open ('<local filename / fullpath_filename>', ' wb ') as f: 
	c.retrbinary (' RETR <remote ftpserver filename> ', f.write) 
# Directly use the host, user, and passwd parameters to initialize FTP, which is equivalent to executing FTP.connect (). login (), which is simple It is generally not so troublesome to write 
# f.closed is True to know that the file has been automatically closed

The FTP class supports the with syntax of the Python context. This syntax is generally recommended in Python and can help you automatically handle the context of related objects, that is, it can help you automatically close related objects and prevent you from forgetting to quit or close.

The two main parameters of the method at the beginning of retr are cmd and callback. The former is the command to get the file, the format is "RETR filename", and the latter is callback is the callback function, generally openfile.write, which means that the relevant data stream is written to an File, the way the file is opened depends on the retr method used, for example, retrbinary () can use 'wb' to open the file.

FTP data upload:

The corresponding data upload functions are the two functions starting with stor: storbinary and storlines. It is still recommended to use the former. Binary is always more trusted.

from ftplib import FTP
with FTP('<ftp server IP>','user','passwd') as c,\
	open('<remote ftpserver filename>','wb') as f:
	c.storbinary('STOR <local filename/fullpath_filename>',f)

Similarly, the CMD format of the uploaded file is "STOR file name".

It should be noted that the retr function uses the callback function to write the file to be downloaded to the local open file, and the stor function opens a file on the server side and then writes the data to be uploaded.

other:

For the common returncode of FTP server, refer to: https://kb.globalscape.com/Knowledgebase/10142/FTP-Status-and-Error-Codes

Guess you like

Origin www.cnblogs.com/leohahah/p/12704292.html