2016-09-27 35 views
0

我使用Python的FTPLIB时上传的小文本文件到FTP站点时,收到以下错误:无法通过ftp使用python FTPLIB文件,但成功的使用curl

 File "ftpuploader.py", line 13, in uploadFilePath 
    ftp.storbinary("STOR {}".format(filepath), file) 
    File "/usr/lib64/python2.7/ftplib.py", line 471, in storbinary 
    conn = self.transfercmd(cmd, rest) 
    File "/usr/lib64/python2.7/ftplib.py", line 376, in transfercmd 
    return self.ntransfercmd(cmd, rest)[0] 
    File "/usr/lib64/python2.7/ftplib.py", line 358, in ntransfercmd 
    resp = self.sendcmd(cmd) 
    File "/usr/lib64/python2.7/ftplib.py", line 249, in sendcmd 
    return self.getresp() 
    File "/usr/lib64/python2.7/ftplib.py", line 224, in getresp 
    raise error_perm, resp 
ftplib.error_perm: 553 Could not create file. 

我成功地使用下面的代码当连接到另一个系统时。我可以登录,更改目录,但无法创建文件。我可以加载使用FileZilla的两种或简单curl命令文件,卷曲-T '/路径/到/文件' ftp://192.168.18.75 --user管理员:密码

ftp = FTP(address) 
ftp.login(username, password) 
ftp.cwd('/gui') 
file = open(filepath, 'rb') 
ftp.storbinary("STOR {}".format(filepath), file) 
ftp.retrlines('LIST') # list directory contents  
file.close() 
ftp.quit() 

什么想法?

+0

试试这个,让我知道,如果它的工作原理: ftp.storbinary( 'STOR文件路径',打开(文件路径,“RB ')) –

+1

'filepath'是否只包含文件名?如果不是,请尝试'“STOR {}”。格式(os.path.basename(filepath))'。 – acw1668

+0

如果你提供的不仅仅是“无法ftp”,它会有帮助,即你得到的错误信息或类似的“超时”或类似的东西。它可能与被动模式和主动模式有关,但是不可能说没有错误描述。 –

回答

2

您正在将路径传递到STOR命令的本地路径(与open(filepath, 'rb')使用的路径相同)。

使用CURL时,您不指定任何路径到远程文件。所以文件被上传到当前的FTP工作目录。

正如@ acw1668在评论中已经建议,使用:

ftp.storbinary("STOR {}".format(os.path.basename(filepath)), file) 
+0

一个额外的评论,我不得不在Python中创建ftplib ftp连接时显式设置被动模式为True。该文档提到它默认启用,但无论文档如何,我都必须声明该模式是明确被动的。 https://docs.python.org/2/library/ftplib.html – kernelK