Flask request without port in url

13,005

Solution 1

The run method takes a port optional argument:

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=80)

You can do this for testing, but for production I highly recommend you read the deployment options section in the documentation which details ways to run flask with various front end WSGI servers.

If you need help understanding how all these components work together and how to set them up; this gist has a nice summary.

Update: The host param needs to be a string.

Solution 2

You need sudo privilege in order to use port 80.

sudo python3 app.py 

That will solve the problem.

Share:
13,005
postelrich
Author by

postelrich

I help create applications that increase the user's productivity. Pandas-like data manipulation: http://blaze.readthedocs.io/en/latest/index.html Pure python parallel and distributed computing: http://dask.readthedocs.io/en/latest/

Updated on June 04, 2022

Comments

  • postelrich
    postelrich over 1 year

    I have an ubuntu EC2 server and want to run a flask server. I want to hit the server using my domain name, api.example.com, without having to include the port number. Right now, I can successfully access the server by doing api.example.com:5000/... but I can't figure out how to do api.example.com/....

    Right now I'm just running the flask server directly, using python flask_server.py.

    In flask_server.py:

    if __name__ == '__main__':
        app.run(host=0.0.0.0)
    
  • postelrich
    postelrich about 10 years
    I've tried to set the port to 80, first I get permission denied. When I run with sudo I get an on not being able to import the config module which doesn't make much sense. I've tried nginx, apache, and mod_wsgi to no avail, can't get those to work.
  • Burhan Khalid
    Burhan Khalid about 10 years
    Only root can bind to ports < 1024, and the reason its not working on sudo is probably due to path or Python environment issues. Not sure what you have tried; but simply - install nginx as root, to make sure its running on port 80 (the default). In this configuration your server will show the nginx page on http://api.example.com/; next install uwsgi and then follow this guide.
  • postelrich
    postelrich about 10 years
    ok I got the uwsgi to run but where do I place the nginx config and am I supposed to substitute your application with something? Thanks for the help by the way!
  • Burhan Khalid
    Burhan Khalid about 10 years
    Have you read the documentation? It shows you what to change. The configuration will be in /etc/nginx/sites-available if you are on a debian-based system (like ubuntu).
  • postelrich
    postelrich about 10 years
    Ok I added it to the default file in sites-available, but get 502 Bad Gateway nginx 1.1.19
  • Burhan Khalid
    Burhan Khalid about 10 years
    Buddy, you need to read the documentation - the reason you are getting bad gateway is because you need to run in the uwsgi process - that's what nginx will connect to; and the uwsgi process will run your flask code.
  • Burhan Khalid
    Burhan Khalid about 10 years
    Yeah but are you reading the right documentation and more importantly understanding what you are reading :) Post a (separate) question if something in the documentation is not clear to you.
  • jds
    jds almost 8 years
    This is incorrect. According to Flask documentation: "Defaults to 5000 or the port defined in the SERVER_NAME config variable if present."
  • Raul Marquez
    Raul Marquez almost 2 years
    In my case it runs like this: * Running on localhost:80 (Press CTRL+C to quit). How can we make it like this? * Running on localhost (Press CTRL+C to quit) (i.e. NO port in url)