1

I have a Flask project working well on local machine. Now, I am trying to deploy it on Microsoft Azure server (Linux 17), but when I click on a button, it gives me an error.

@app.route('/database', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save(werkzeug.secure_filename(f.filename))
        Finaldata = readFile(f.filename) # a function to read the uploaded file, request and API then process the data. Create a new file and write the output to
        filename = Finaldata

        @after_this_request
        def remove_file(response):
            try:
                os.remove(f.filename)
                # readDate.readerIn.close()
            except Exception as error:
                app.logger.error("Error removing or closing downloaded file handle", error)
            return response


        def download(response):
            response = make_response(Finaldata)
            response.headers["Content-Disposition"] = "attachment; filename=result.txt"
            render_template('upload.html', filename=filename)
            return response
        return render_template('upload.html', filename = filename)

My HTML code:

<form action = "/database" method = "POST"
     enctype = "multipart/form-data" class="w3-button w3-light-grey w3-padding-large w3-margin-top">
     <input type = "file" name = "file" />
     <input type = "submit"/>
  </form>

Error message on the browser:

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Error message in the logs:

[Tue Apr 17 17:40:07.103627 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358] [2018-04-17 17:40:07,100] ERROR in app: Exception on /database [POST], referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103675 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358] Traceback (most recent call last):, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103685 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103693 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]     response = self.full_dispatch_request(), referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103701 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103709 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]     rv = self.handle_user_exception(e), referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103717 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103725 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]     reraise(exc_type, exc_value, tb), referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103795 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]   File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103806 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]     raise value, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103813 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103821 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]     rv = self.dispatch_request(), referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103829 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request, referer: http://52.186.71.2/
[Tue Apr 17 17:40:07.103836 2018] [wsgi:error] [pid 31304:tid 140188999214848] [client 134.193.130.121:41358]     return self.view_functions[rule.endpoint](**req.view_args), referer: http://52.186.71.2/
SineCo
  • 33
  • 1
  • 2
  • 10
  • You have taken your log output from a console that *truncates the data*. We can only see the WSGI server prefixes to the lines (such as the PID and IP address), but the information we need has been mostly truncated. – Martijn Pieters Apr 17 '18 at 17:51
  • I can *just* about make out there is a permission error with some kind of file. IS your code trying to save something on the server? Then your process does not have permission to do so. – Martijn Pieters Apr 17 '18 at 17:51
  • You want to read http://flask.pocoo.org/docs/0.12/patterns/fileuploads/#a-gentle-introduction carefully; you are trying to save the files relative to the current working directory, *and you usually don't know where that is* on a WSGI server. Use an absolute path instead. The linked page uses an explicit `app.config['UPLOAD_FOLDER']` path. – Martijn Pieters Apr 17 '18 at 17:53
  • @MartijnPieters That is what is the server provided for me! – SineCo Apr 17 '18 at 17:54
  • Thank you so much. But why is there a permission error while I am running it as the root? – SineCo Apr 17 '18 at 17:55
  • The server would not have provided the `$` entries on the right. – Martijn Pieters Apr 17 '18 at 17:55
  • So I do not have the permission to save a file on the server ? – SineCo Apr 17 '18 at 17:55
  • Your WSGI process won't be running as root, at least, I'd hope not. – Martijn Pieters Apr 17 '18 at 17:55
  • You are saving your files in the *wrong place*. – Martijn Pieters Apr 17 '18 at 17:55
  • 1
    To determine where you *should* save your files, take a look at the [Filesystem Hierarchy Standard](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard). It gives a rough map of how most Linux filesystems are laid out. You don't have to memorize it, but use it as a reference. – Patrick Haugh Apr 17 '18 at 18:00
  • @MartijnPieters Thank you. I fixed the server error message – SineCo Apr 17 '18 at 18:03

0 Answers0