简体   繁体   English

Python ftplib错误553

[英]Python ftplib error 553

I am attempting to use python to connect to a server and upload some files from my local directory to /var/www/html but every time I try to do this I get this error: 我试图使用python连接到服务器并将我的本地目录中的一些文件上传到/ var / www / html但每次我尝试这样做时都会收到此错误:

Error: ftplib.error_perm: 553 Could not create file. 错误:ftplib.error_perm:553无法创建文件。

I have already did a chown and a chmod -R 777 to the path. 我已经在路径上做了一个chown和一个chmod -R 777。 I am using vsftpd and already set write enabled. 我正在使用vsftpd并已启用写入启用。 Does anyone have any ideas? 有没有人有任何想法?

Code: 码:

ftp = FTP('ipaddress')
ftp.login(user='user', passwd = 'user')
ftp.cwd('/var/www/html')
for root, dirs, files in os.walk(path):
    for fname in files:
        full_fname = os.path.join(root, fname)
        ftp.storbinary('STOR' + fname, open(full_fname, 'rb'))

I had a similar problem also getting the error 553: Could not create file . 我有类似的问题也得到错误553: Could not create file What ( update : partially) solved it for me was changing this line from: 什么( 更新 :部分)为我解决了改变这一行:

ftp.storbinary('STOR' + fname, open(full_fname, 'rb'))

to: 至:

ftp.storbinary('STOR ' + '/' + fname, open(full_fname, 'rb'))

Notice that there is a space just after the 'STOR ' and I added a forward slash ('/') just before the filename to indicate that i'd like the file stored in the FTP root directory 请注意,在'STOR'之后有一个空格 ,我在文件名之前添加了正斜杠('/'),表示我想要存储在FTP根目录中的文件

UPDATE: [2016-06-03] Actually this only solved part of the problem. 更新: [2016-06-03]实际上这只解决了部分问题。 I realized later that it was a permissions problem. 我后来意识到这是一个权限问题。 The FTP root directory allowed writing by the FTP user but i had manually created folders within this directory using another user thus the new directories did not allow the FTP user to write to these directories. FTP根目录允许FTP用户写入,但我使用另一个用户在此目录中手动创建了文件夹,因此新目录不允许FTP用户写入这些目录。

Possible solutions: 可能的解决方案:

  1. Change the permissions on the directories such that the FTP user is the owner of these directories, or is at least able to read and write to them. 更改目录的权限,使FTP用户是这些目录的所有者,或者至少能够读取和写入这些目录。
  2. Create the directories using the ftp.mkd(dir_name) function, then change directory using the ftp.cwd(dir_name) function and then use the appropriate STOR function ( storlines or storbinary ) to write the file to the current directory. 创建使用目录ftp.mkd(dir_name)函数,然后使用更改目录ftp.cwd(dir_name)函数,然后使用适当的STOR功能( storlinesstorbinary )将文件写入到当前目录。

    • As far as my understanding goes, the STOR command seems to only take a filename as a parameter (not a file path), that's why you need to make sure you are in the correct 'working directory' before using the STOR function (Remember the space after the STOR command) 据我所知,STOR命令似乎只将文件名作为参数(不是文件路径),这就是为什么在使用STOR函数之前需要确保你在正确的“工作目录”中(记住STOR命令后的空格)

      ftp.storbinary('STOR ' + fname, open(full_fname, 'rb'))

Does path == '/var/www/html' ? path == '/var/www/html' That's a local path. 这是一条本地路径。 You need an FTP path. 您需要一个FTP路径。

The local path /var/www/html is not generally accessible by FTP. FTP通常不能访问本地路径/var/www/html When you connect to the FTP server, the file system presented to you begins at, often, your user's home directory /home/user . 当您连接到FTP服务器时,呈现给您的文件系统通常您的用户的主目录/home/user

Since it sounds like you're running the ftp server (vsftpd) on the remote machine, the simplest solution might be something like: 因为听起来你在远程机器上运行ftp服务器(vsftpd),最简单的解决方案可能是这样的:

user@server:~$ ln -s /var/www/html /home/user/html

Then you could call ftp.cwd('html') , and ftp.nlst() to get the remote directory listing, and navigate it from there. 然后你可以调用ftp.cwd('html')ftp.nlst()来获取远程目录列表,并从那里导航它。

Also, don't forget to put a space character in the 'STOR' string (should be 'STOR ' ). 另外,不要忘记在'STOR'字符串中添加空格字符(应该是'STOR ' )。

Best of luck! 祝你好运!

I'm sure at this point you have found a solution, but I just stumbled across this thread while I was looking for a solution. 我确信你已经找到了解决方案,但我在寻找解决方案时偶然发现了这个问题。 I ended up using the following: 我最终使用了以下内容:

# Handles FTP transfer to server
def upload(ftp, dir, file):
    # Test if directory exists. If not, create it
    if dir.split('/')[-1] not in ftp.nlst('/'.join(dir.split('/')[:-1])):
        print("Creating directory: " + dir)
        ftp.mkd(dir)
    # Check if file extension is text format
    ext = path.splitext(file)[1]
    if ext.lower() in (".txt", ".htm", ".html"):
        ftp.storlines("STOR " + dir + '/' + file, open(dir + '/' + file, "rb"))
    else:
        ftp.storbinary("STOR " + dir + '/' + file, open(dir + '/' + file, "rb"), 1024)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM