vivek111
vivek111

Reputation: 1

permission denied error while creating a file in python flask on apache and mod_wsgi

I am trying to create a csv file through python flask. The code works fine in localhost but gives a permission denied error when deployed on apache + mod_wsgi on aws ec2 instance.

@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)

The app configuration file is :

   <VirtualHost *:80>
       ServerName example1.example.com
       ServerAdmin [email protected]
       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>

The wsgi config file is : import sys import logging

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"

Error :

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

Upvotes: 0

Views: 1478

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58563

You can't use a relative path name as the working directory of the process will not be where your code is. This, along with what you need to do is explained in the mod_wsgi documentation at:

Upvotes: 1

Related Questions