Changing permissions via ftp in python

I am using python with ftplib to upload images to a folder on my raspberryPi located in / var / www. Everything works fine, except the downloaded files have 600 permissions and I need 644 for them.

What is the best way to do this? I am looking 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() 
+5
source share
2 answers

You need to use sendcmd.

Here is an example 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!

+5
source

I used 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 as follows:

 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

+2
source

Source: https://habr.com/ru/post/1202359/


All Articles