【问题标题】:Change permissions via ftp in python在python中通过ftp更改权限
【发布时间】:2014-11-05 17:33:04
【问题描述】:

我正在使用带有ftplib 的python 将图像上传到我的树莓派上位于/var/www 的文件夹中。 一切正常,除了上传的文件有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()

【问题讨论】:

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

标签: python permissions ftplib


【解决方案1】:

您需要使用 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()

编程愉快!

【讨论】:

    【解决方案2】:

    对于这种情况,我会在 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)
    

    chmod 方法与os.chmod 具有相同的语义

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 2012-11-14
      • 1970-01-01
      • 2020-07-04
      • 2013-04-21
      • 2010-12-08
      相关资源
      最近更新 更多