问题描述
我正在从 C-More 工业 HMI FTP 服务器下载两个文件。我不知道 HMI 运行的是什么操作系统,但我怀疑它的 FTP 服务器有一些怪癖。使用 Jython 2.7,可以毫无困难地读取一个文件,但另一个文件的文件名中有一个空格,并且普通的引号括起来的解决方案还不起作用。
以下适用于 Windows 10 FTP 客户端。
ftp> get NO_SPACES.csv
200 PORT command successful.
150 opening ASCII mode data connection for NO_SPACES.csv.
226 Transfer complete.
ftp: 12774 bytes received in 0.27Seconds 47.66Kbytes/sec.
ftp> get "WITH SPACE.csv"
200 PORT command successful.
150 opening ASCII mode data connection for WITH SPACE.csv.
226 Transfer complete.
ftp: 6328 bytes received in 0.02Seconds 316.40Kbytes/sec.
到目前为止,一切都很好。现在用 Python 试试:
ftp = FTP(myIP) # Connect.
ftp.login(userName,password) # Login.
ftp.set_pasv(False) # required by the C-More panel for some reason.
with io.BytesIO() as binary_buffer:
# read all of products into a binary buffer
# ftp.retrbinary("RETR NO_SPACES.csv",binary_buffer.write) # This line works.
ftp.retrbinary('RETR "WITH SPACE.csv"',binary_buffer.write) # This one doesn't.
我的开发系统中的脚本控制台报告:
ftplib.error_perm: 550 "WITH SPACE.csv": 请求的操作未采取。
- 已更改文件名以保护无辜者。
- Windows FTP 喜欢
get
命令。 Python 似乎偏爱RETR
。 - 我试过
'RETR "WITH SPACE.csv"'
和"RETR 'WITH SPACE.csv'"
。结果相同。 - 如果需要,我可以重命名 HMI 中的文件,但这需要一些验证和文书工作,这并不好玩。
- 我正在使用 Jython 2.7 的 Inductive Automation 的 Ignition! SCADA 系统的最新版本进行开发。
有人对我有什么想法可以尝试吗?
解决方法
ftplib 没有空格问题。问题在于您添加到 RETR
命令的引号。不应该有引号:
ftp.retrbinary('RETR WITH SPACE.csv',binary_buffer.write)
如果您使用 ftp
开关在 -d
中启用调试模式,您将看到它也不会在 RETR
命令中向 FTP 服务器发送引号:
ftp> get "WITH SPACE.csv"
---> PORT 127,1,15,145
200 Port command successful
---> RETR WITH SPACE.csv
150 Opening data channel for file download from server of "/WITH SPACE.csv"
226 Successfully transferred "/WITH SPACE.csv"
ftp: 12 bytes received in 0.00Seconds 12000.00Kbytes/sec.
请注意,get
是一个命令行 ftp
客户端 user 命令,可转换为 FTP protocol RETR
命令。