Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中通过ftp更改权限_Python_Permissions_Ftplib - Fatal编程技术网

在python中通过ftp更改权限

在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

我正在使用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()

我将使用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()

快乐编程

如果您在下面找到了正确答案,请务必将其标记为正确。