"Permission denied" while using flask

Question:

I am trying to make a messenger bot using Wit.ai. For that I need a webhook. I am using ngrok, and this is just a test file but I am getting this error.

This is the error I am getting.

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
Traceback (most recent call last):
  File "app.py", line 56, in <module>
    app.run(debug = True, port = 80)
  File "/home/parth/.local/lib/python3.6/site-packages/flask/app.py", line 990, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 988, in run_simple
    s.bind(server_address)
PermissionError: [Errno 13] Permission denied

This is the code

import os, sys
from flask import Flask, request

app = Flask(__name__)
@app.route('/', methods=['GET'])
def verify():
    # Webhook verification
    if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
        if not request.args.get("hub.verify_token") == "hello":
            return "Verification token mismatch", 403
        return request.args["hub.challenge"], 200
    return "Hello world", 200


if __name__ == "__main__":
    app.run(debug = True, port = 80)

Thanks!

Asked By: parth shukla

||

Answers:

You’re getting that error because you’re using a Privileged Port.

The TCP/IP port numbers below 1024 are special in that normal users
are not allowed to run servers on them.

When you run a server as a test from a non-priviliged account, you
will normally test it on other ports, such as 2784, 5000, 8001 or
8080.

Change your port to 5000, for example, and that will fix your problem.

Answered By: Tiago Martins Peres

Changing the port number is the better solution. If you can do that, don’t read this. This solution opens up routes for gaining root control of your server.

However to run on a priveleged port, you don’t need to sudo pip install. In case you missed the first para, never do this in production or on any server within a secure environment.

That said, you can just run with sudo, from a non-root installed venv, using:

sudo /path/to/venv/bin/python application.py

or, if you don’t have the if __name__ == "__main__" bit:

sudo FLASK_APP=application.py /path/to/venv/bin/flask run --host 0.0.0.0 --port=80

I pip installed with a non-root user, which is slightly safer.

Answered By: Chris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.