Question

I'm learning web programming with Python, and still basically going through lectures/tutorial.

I'm trying to upload a file to a server. This is my code:

import ftplib
import sys

filename = sys.argv[1]
connect = ftplib.FTP("***.**.***.**")
connect.login("testuser","pass")
file = open(filename, "rb")
connect.storbinary("STOR " + filename, file)
connect.quit()

and this is the error I have:

File "C:\Users\test\putfile.py", line 8, in <module>
   connect.storbinary("STOR " + filename, file)
File "C:\Python27\lib\ftplib.py", line 471, in storbinary
   conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
   return self.ntransfercmd(cmd, rest)[0]
 File "C:\Python27\lib\ftplib.py", line 339, in ntransfercmd
   resp = self.sendcmd(cmd)
 File "C:\Python27\lib\ftplib.py", line 249, in sendcmd
   return self.getresp()
 File "C:\Python27\lib\ftplib.py", line 224, in getresp
   raise error_perm, resp
 ftplib.error_perm: 550 Permission denied.

testuser should have the permission to write files, since the folder is owned by him, and he has root privilege(was added in sudoer file).

the same thing happens if I add the line:

 connect.cwd('/testfolder')

I will get error_perm: 550 Failed to change directory.

However I can still read the existing files just fine (with connect.retrlines("RETR " + filename))

I'm pretty new about Python as well as Linux, so I don't have idea what I'm doing. I need some help.

No correct solution

OTHER TIPS

Perhaps this can help:

With FTP is not sufficient be owner of files and directories. The service and daemon FTP must be correctly configured in order to write and create files etc.

For example in Ubuntu:
Edit /etc/vsftpd.conf

And in the line

;write_enable=YES

Delete the semicolon

Finally restart the service:

sudo service  vsftpd restart

I would check if you are in the right location. I got the same problem, and then I realised that I was in a different location that I intended, in the root folder, above "/public_html", so there was no folder that I wanted to enter, and I didn't have permissions to store any files.

You can check where you are this way:

print connect.pwd()

and what the contents of the current directory are:

print ftplib.FTP.dir(connect)

So, if you are in the root folder ("/"), above the "/public_html" and you want to change current directory to "/testfolder" you need to use:

connect.cwd('/public_html/testfolder')

Have you checked the access permission on the FTP server? I just ran into this same problem. This issue happened cause I did not have the permission to read the folder that I want to upload my files into.

There are a few things one can check if encountered with this error.

  1. Check the current directory of the ftp server that you are trying to access using connect.pwd(). Make sure you have the write access to this directory. You can try copy pasting manually to verify the same.
  2. Make sure you only provide the filename and not the complete path. For me, this was causing an issue. For example, filename = "upload_img.jpg" instead of filename = "D:/apth/to/upload_img.jpg". Workaround will be to extract the CWD using os.split() and then set the CWD using the os.chdir()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top