在apache和mod_wsgi上的python flask中创建文件时出现权限被拒绝错误 [英] permission denied error while creating a file in python flask on apache and mod_wsgi

查看:19
本文介绍了在apache和mod_wsgi上的python flask中创建文件时出现权限被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过python flask.创建一个CSV文件。该代码在本地主机中运行良好,但在AWS EC2实例上部署在apache+mod_wsgi上时会出现权限被拒绝错误。

@app.route('/downloadChats/<db_token>/<requestrange>/', methods = ['GET'])
def downloadChats(db_token, requestrange):

   <fetch data from table>

    try:
            filename = str(time.time()).strip().split('.')[0] + '.csv'
            df = pd.DataFrame(columns = ['sessionid', 'city', 'ipAddress', 'startTime', 'timestamp', 'name', 'mob_no'])
            for sessid in sessionids:
                    df = df.append({'sessionid' : disp_json[sessid]['sessionid'], 'city' : disp_json[sessid]['city'], 'ipAddress' : disp_j$
            df.to_csv(filename, index = False)
    except Exception as msg:
            print("Exception while writing csv file in downloadChats : " + str(msg))
            return "<h1> THis service is unavailable currently, Please try later. </h1> "
    return send_from_directory('/var/www/FlaskApps/chatbotApp/', filename, as_attachment=True)

APP配置文件为:

   <VirtualHost *:80>
       ServerName example1.example.com
       ServerAdmin admin@mywebsite.com
       WSGIScriptAlias / /var/www/FlaskApps/FlaskApps.wsgi
       WSGIPassAuthorization On
       <Directory /var/www/FlaskApps/chatbotApp/>
         Order allow,deny
         Allow from all
         </Directory>
       <Directory /var/www/FlaskApps/chatbotApp/static/>
         Order allow,deny
         Allow from all
       </Directory>

       ErrorLog ${APACHE_LOG_DIR}/error.log
       LogLevel warn
       CustomLog ${APACHE_LOG_DIR}/access.log combined
       RewriteEngine on
       RewriteCond %{SERVER_NAME} = example1.example.com
       RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} 
       [END,NE,R=permanent]
    </VirtualHost>
WSGI配置文件为: 导入系统 导入日志记录

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApps/chatbotApp/")

# home points to the home.py file
from home import app as application
application.secret_key = "somesecretsessionkey"

错误:

[Errno 13] Permission denied: '1518497209.csv'

推荐答案

您不能使用相对路径名,因为进程的工作目录将不在您的代码所在的位置。Mod_wsgi文档中对此以及您需要执行的操作进行了说明:

这篇关于在apache和mod_wsgi上的python flask中创建文件时出现权限被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆