在python中通过ftp更改权限
我正在使用python和在python中通过ftp更改权限,python,permissions,ftplib,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
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()
我将使用paramiko中的SFTPClient处理此案例: 您可以像这样连接、打开文件和更改权限:
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)
chmod
方法与需要使用sendcmd的os.chmod
具有相同的语义
以下是通过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()
快乐编程 如果您在下面找到了正确答案,请务必将其标记为正确。