Flask request without port in url

2019-07-18 05:58发布

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)

3条回答
2楼-- · 2019-07-18 06:46

You need sudo privilege in order to use port 80.

sudo python3 app.py 

That will solve the problem.

查看更多
贪生不怕死
3楼-- · 2019-07-18 06:50

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.

查看更多
男人必须洒脱
4楼-- · 2019-07-18 07:00

Correct syntax to use Flask server on port 80 is:

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

Keep in mind that you might need super user privileges.

This kind of workaround wound be acceptable if your were still building your application.

If, as I understood, you plan to deploy your app in production then you'd need to do it properly. Here you find step by step information for Ubuntu, as requested:

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps

查看更多
登录 后发表回答