无法使用python ftplib ftp文件,但使用curl成功 [英] Unable to ftp a file using python ftplib, but successful using curl
问题描述
当使用python的ftplib将小型文本文件上传到ftp站点时,收到以下错误:
I receive the following error when uploading a small text file to an ftp site when using python's ftplib:
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:密码
I am using the following code successfully when connecting to another system. I can log in, change directories, but am unable to create the file. I can load a file using either filezilla or a simple curl command, 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')
).
You are passing a path to a local path to the STOR
command (the same path you use with the open(filepath, 'rb')
).
使用CURL时,您不指定远程文件的任何路径.这样文件便被上传到当前的FTP工作目录.
While with the CURL, you do not specify any path to the remote file. So the file gets uploaded to the current FTP working directory.
如@ acw1668的注释中所建议,请使用:
As already suggested in the comments by @acw1668, use the:
ftp.storbinary("STOR {}".format(os.path.basename(filepath)), file)
这篇关于无法使用python ftplib ftp文件,但使用curl成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!