我通过 python 公开了一个 API,通过 ftplib 连接到我的 ftp 服务器:
from ftplib import FTP
@app.route('/api/ftp/list', methods=['GET'])
def ftp_list():
remote_dir = request.args.get('remote_dir', '/')
try:
ftp = ftp_connect()
#ftp.cwd(remote_dir)
files = ftp.nlst()
ftp.quit()
return jsonify({'files': files}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
它通过 NLST 方法成功检索文件列表。
当我通过 Windows 命令行连接到我的 ftp 服务器时,我无法运行 NLST 方法:
ftp> dir
200 'PORT' OK.
125 Data connection already open; starting 'BINARY' transfer.
-rw------- 1 tester users 95 Sep 19 20:34 testfile.txt
226 Transfer complete.
ftp: 67 bytes received in 0.00Seconds 67000.00Kbytes/sec.
ftp> nlst
Invalid command.
ftp>
NLST
是 FTP 协议命令。与任何其他协议(或其他协议)的任何其他实现一样,不同的(FTP)客户端/库将具有不同的 API 用于访问功能:
FTP.nlst()
方法
ftp
客户端有
ls
命令
NLST
FTP 命令,因为 FTP 协议中的所有文件传输和列表都是复杂的操作。它们涉及多个命令并打开单独的传输连接。
NLST
只是整个操作的一个(也是最重要的)部分。