在apache和mod_wsgi上的python烧瓶中创建文件时,权限被拒绝

时间:2018-02-13 05:48:02

标签: mod-wsgi flask-restful

我正在尝试通过python flask创建一个csv文件。代码在localhost中正常工作,但在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)

应用配置文件是:

   <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'

1 个答案:

答案 0 :(得分:0)

您不能使用相对路径名,因为进程的工作目录不在您的代码所在的位置。这个以及您需要做的事情在mod_wsgi文档中解释: