6

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

2 Answers2

9

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!

Software Prophets
  • 2,838
  • 3
  • 21
  • 21
5

I would use the SFTPClient in paramiko for this case: http://paramiko-docs.readthedocs.org/en/latest/api/sftp.html

You can connect, open the file, and change permissions like this:

import paramiko, stat

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(your_hostname,
               username=user,
               password=passwd)

sftp = client.open_sftp()
remote = sftp.file(remote_filename, 'w')
#remote.writes here
# Here, user has all permissions, group has read and execute, other has read
remote.chmod(stat.S_IRWXU | stats.S_IRGRP | stats.S_IXGRP
             | stats.IROTH)

The chmod method has the same semantics as os.chmod

talumbau
  • 114
  • 3