我正在尝试使用TLS连接到FTP服务器并上传文本文件。以下代码连接到网站就好了,但它没有上传文件。相反,我收到以下错误:
Traceback (most recent call last):
File "X:/HR & IT/Ryan/Python Scripts/ftps_connection_test.py", line 16, in <module>
ftps.storlines("STOR " + filename, open(filename,"r"))
File "C:\Python33\lib\ftplib.py", line 816, in storlines
with self.transfercmd(cmd) as conn:
File "C:\Python33\lib\ftplib.py", line 391, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python33\lib\ftplib.py", line 756, in ntransfercmd
conn, size = FTP.ntransfercmd(self, cmd, rest)
File "C:\Python33\lib\ftplib.py", line 357, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python33\lib\ftplib.py", line 264, in sendcmd
return self.getresp()
File "C:\Python33\lib\ftplib.py", line 238, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 The parameter is incorrect.
我可能缺少一些非常基本的东西,我的代码在下面,非常感谢任何帮助。
import os
from ftplib import FTP_TLS as f
# Open secure connection
ftps = f("ftp.foo.com")
ftps.login(username,password)
ftps.prot_p()
# Create the test txt file to upload
filename = r"c:\path\to\file"
testFile = open(filename,"w")
testFile.write("Test file with test text")
testFile.close()
# Transfer testFile
ftps.storlines("STOR " + filename, open(filename,"r"))
# Quit connection
ftps.quit()
答案 0 :(得分:1)
尝试将文件上传到FTP服务器时,我遇到了同样的错误。就我而言,目标文件名不是正确的格式。它就像是
data_20180411T12:00:12.3435Z.txt
我重命名了
data_20180411T120012_3435Z.txt
。然后它奏效了。
答案 1 :(得分:0)
filename = r"c:\path\to\file"
是本地文件的绝对路径。在STOR
命令中传递相同的值,即
ftps.storlines("STOR " + filename, open(filename,"r"))
尝试执行STOR c:\path\to\file
操作,但远程服务器上的路径不太可能,ftplib.error_perm
例外表明您没有权限写在那里(即使它确实存在)。
您可以尝试这样做:
ftps.storlines("STOR " + os.path.basename(filename), open(filename,"r"))
将发出STOR file
操作并将文件上载到远程服务器上的默认目录。如果需要上传到远程服务器上的其他路径,只需将其添加到STOR即可。