Nephie
Nephie

Reputation: 67

Flask - Errno 13 premission denied

I'm writing a small web page whose task is to let a user upload his input file and with uploading I want to execute my calculation program in python which will give me output for the user.

My code looks like this:

import os
import os.path
import datetime
import subprocess
from flask import Flask, render_template, request, redirect, url_for
from werkzeug import secure_filename

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'gro', 'doc', 'docx'])

current_time = datetime.datetime.now()
file_time = current_time.isoformat()
proper_filename = file_time

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']

def run_script():
    subprocess.call(['/home/martyna/Dropbox/programowanie/project_firefox/topologia.py', '/uploads/proper_filename'])

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/upload', methods = ['POST'])
def upload():
    file = request.files['file']
    if file and allowed_file(file.filename):
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], proper_filename))
        run_script().start()
        return "Thank you for uploading"


if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0')

Uploading goes well, but the problem is that when I hit upload I get message OSError: [Errno 13] Permission denied and the line causing the problem is:

subprocess.call(['/home/martyna/Dropbox/programowanie/project_firefox/topologia.py', '/uploads/2014-05-16T22:08:19.522441'])

program topologia.py runs from command python topologia.py input_file

I have no idea how to solve that problem.

Upvotes: 2

Views: 3182

Answers (2)

daouzli
daouzli

Reputation: 15328

Executing a script from a command line and from a server will not be done with the same permissions.

user@mycomputer:~$ ./script

In this exemple, ./script is launched by user. So if it does some inputs/outputs, the access rigths will depend on user rights.

When it is a server that runs the script, in your case Flask, it is probably www-data that launch the script. So the access rights are not the same.

So to create a file into a folder the user executing the script should have the permissions on the folder.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1123480

You have two problems:

  • Your script is probably not marked as executable. You can work around that by using the current Python executable path; use sys.executable to get the path to that.

  • You are telling the script to process /uploads/proper_filename, but the filename you actually upload your file to is not the same at all; you should use the contents of the string referenced by proper_filename instead.

Put these two together:

import sys
from flask import current_app

def run_script():
    filename = os.path.join(current_app.config['UPLOAD_FOLDER'], proper_filename)
    subprocess.call([
        sys.executable, 
        '/home/martyna/Dropbox/programowanie/project_firefox/topologia.py', 
        filename])

You do not need to call .start() on the result of run_script(); you'll get an attribute error on NoneType. Just call run_script() and be done with it:

run_script()

Upvotes: 2

Related Questions