无法使用 python ftplib ftp 文件,但使用 curl 成功

Unable to ftp a file using python ftplib, but successful using curl

我在使用 python 的 ftplib 将小文本文件上传到 ftp 站点时收到以下错误:

      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 命令加载文件,curl -T '/path/to/file' ftp://192.168.18.75 --user admin:password

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()

有什么想法吗?

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

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

正如@acw1668 在评论中所建议的那样,使用:

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