Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change permissions via ftp in python

I'm using python with ftplib to upload images to a folder on my raspberryPi located in /var/www. Everything is working fine except that uploaded files have 600 permissions and I need 644 for them.

Which is the best way to do this? I'm searching for something like:

def ftp_store_avatar(name, image):
    ftp = ftp_connect()
    ftp.cwd("/img")
    file = open(image, 'rb')
    ftp.storbinary('STOR ' + name + ".jpg", file)     # send the file

    [command to set permissions to file]

    file.close()
    ftp.close()
like image 454
Leonardo Romanello Avatar asked Sep 11 '14 16:09

Leonardo Romanello


People also ask

How do I change permissions on an FTP?

Right click on the file name and select "File permissions" from the drop down menu. In the Change file attributes window that opens, change the "Numeric value" permission to 644 to give the Owner permission to Read, Write, and Execute. Click on OK to continue.

How do you change file or directory permissions via FTP?

Right-click on the name of the folder/file you want to change the permissions for and click on File Permissions. A new window will pop-up. In it, use the checkboxes to set the desired permissions or alternatively use the Numeric value text box and input the numeric value of the desired permissions.

How do you change permissions on a Python script?

Use os.chmod(path, mode) to change the file permissions to mode for the file located at path . To set multiple permissions, use the OR | operator to combine permission flags. Further reading: There are many different flags in the stat module.


1 Answers

You need to use sendcmd.

Here is a sample program that changes permissions via ftplib:

#!/usr/bin/env python

import sys
import ftplib

filename = sys.argv[1]
ftp = ftplib.FTP('servername', 'username', 'password')
print ftp.sendcmd('SITE CHMOD 644 ' + filename)
ftp.quit()

Happy programming!

like image 95
Software Prophets Avatar answered Sep 30 '22 07:09

Software Prophets