繁体   English   中英

授予在 Apache2 Z1D41C853AF58D3A77AE54990DCEZ9 上运行的 Python Flask 应用程序的权限

[英]Give permission to Python Flask application running on Apache2 ubuntu

我在 Ubuntu 上的 Apache2 服务器上运行 Flask 应用程序。 该应用程序将从表单中获取输入并将其保存到文本文件中。 该文件仅在上传到 S3 时存在。 之后它被删除。:

            foodforthought = request.form['txtfield']
        with open("filetos3.txt", "w") as file:
            file.write(foodforthought)
        file.close()
        s3.Bucket("bucketname").upload_file(Filename = "filetos3.txt", Key = usr+"-"+str(datetime.now()))
        os.remove("filetos3.txt")

但该应用无权创建文件:

[Errno 13] Permission denied: 'filetos3.txt'

我已经尝试授予应用程序所在文件夹的权限:

 sudo chmod -R 777 /var/www/webApp/webApp

但它不起作用

我的猜测是该应用程序是从不同的位置运行的。 你从中得到什么 output :

import os
print(os.getcwd())

这是您需要设置权限的目录。 更好的是,使用绝对路径。 由于该文件是临时文件,请使用此处详述tempfile

foodforthought = request.form['txtfield']

with tempfile.NamedTemporaryFile() as fd:
    fd.write(foodforthought)
    fd.flush()

    # Name of file is in the .name attribute.
    s3.Bucket("bucketname").upload_file(Filename = fd.name, Key = usr+"-"+str(datetime.now()))

    # The file is automatically deleted when closed, which is when the leaving the context manager.

最后一些注意事项:您不需要close文件,因为您使用上下文管理器。 另外,避免递归设置 777。 最安全的方法是设置+wX以便仅在目录上设置execute位并在所有内容上write位。 或者更好的是,更具体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM