Leo*_*llo 5 python permissions ftplib
我正在使用python ftplib将图像上传到位于/ var/www的raspberryPi上的文件夹.一切都工作正常,除了上传的文件有600权限,我需要644它们.
这是最好的方法吗?我正在寻找类似的东西:
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()
Run Code Online (Sandbox Code Playgroud)
您需要使用sendcmd.
这是一个通过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()
Run Code Online (Sandbox Code Playgroud)
编程愉快!
小智 5
对于这种情况,我会在 paramiko 中使用 SFTPClient:http ://paramiko-docs.readthedocs.org/en/latest/api/sftp.html
您可以像这样连接、打开文件和更改权限:
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)
Run Code Online (Sandbox Code Playgroud)
该chmod方法具有相同的语义os.chmod