How to Change the Host and Port That the Flask Command Uses

How can I change the host and port that the flask command uses?

The flask command is separate from the flask.run method. It doesn't see the app or its configuration. To change the host and port, pass them as options to the command.

flask run -h localhost -p 3000

Pass --help for the full list of options.

Setting the SERVER_NAME config will not affect the command either, as the command can't see the app's config.


Never expose the dev server to the outside (such as binding to 0.0.0.0). Use a production WSGI server such as uWSGI or Gunicorn.

gunicorn -w 2 -b 0.0.0.0:3000 myapp:app

How do I get Flask to run on port 80?

So it's throwing up that error message because you have apache2 running on port 80.

If this is for development, I would just leave it as it is on port 5000.

If it's for production either:

Not Recommended

  • Stop apache2 first;

Not recommended as it states in the documentation:

You can use the builtin server during development, but you should use a full deployment option for production applications. (Do not use the builtin development server in production.)

Recommended

  • Proxy HTTP traffic through apache2 to Flask.

This way, apache2 can handle all your static files (which it's very good at - much better than the debug server built into Flask) and act as a reverse proxy for your dynamic content, passing those requests to Flask.

Here's a link to the official documentation about setting up Flask with Apache + mod_wsgi.

Edit 1 - Clarification for @Djack

Proxy HTTP traffic to Flask through apache2

When a request comes to the server on port 80 (HTTP) or port 443 (HTTPS) a web server like Apache or Nginx handles the connection of the request and works out what to do with it. In our case a request received should be configured to be passed through to Flask on the WSGI protocol and handled by the Python code. This is the "dynamic" part.

reverse proxy for dynamic content

There are a few advantages to configuring your web server like the above;

  • SSL Termination - The web server will be optimized to handle HTTPS requests with only a little configuration. Don't "roll your own" in Python which is probably very insecure in comparison.
  • Security - Opening a port to the internet requires careful consideration of security. Flask's development server is not designed for this and could have open bugs or security issues in comparison to a web server designed for this purpose. Note that a badly configured web server can also be insecure!
  • Static File Handling - It is possible for the builtin Flask web server to handle static files however this is not recommended; Nginx/Apache are much more efficient at handling static files like images, CSS, Javascript files and will only pass "dynamic" requests (those where the content is often read from a database or the content changes) to be handled by the Python code.
  • +more. This is bordering on scope for this question. If you want more info do some research into this area.

How to change Flask server host/port using config file?

http://flask.pocoo.org/docs/1.0/config/#SERVER_NAME

Inform the application what host and port it is bound to.

This parameter only inform[s] the application what host and port it is bound to.

From this answer, you can try

flask run --host=127.0.0.23 --port=5001

Note that in production, you should definitely not use the debug server. And in development, the default config is fine for most cases.

Configure Flask dev server to be visible across the network

While this is possible, you should not use the Flask dev server in production. The Flask dev server is not designed to be particularly secure, stable, or efficient. See the docs on deploying for correct solutions.


The --host option to flask run, or the host parameter to app.run(), controls what address the development server listens to. By default it runs on localhost, change it to flask run --host=0.0.0.0 (or app.run(host="0.0.0.0")) to run on all your machine's IP addresses.

0.0.0.0 is a special value that you can't use in the browser directly, you'll need to navigate to the actual IP address of the machine on the network. You may also need to adjust your firewall to allow external access to the port.

The Flask quickstart docs explain this in the "Externally Visible Server" section:

If you run the server you will notice that the server is only
accessible from your own computer, not from any other in the network.
This is the default because in debugging mode a user of the
application can execute arbitrary Python code on your computer.

If you have the debugger disabled or trust the users on your network,
you can make the server publicly available simply by adding
--host=0.0.0.0 to the command line:

$ flask run --host=0.0.0.0

This tells your operating system to listen on all public IPs.

how can I run flask app in different port?

There are a couple of options that might help.

If you are running your app using the flask run command in the terminal try adding this to the command

flask run -h localhost -p 8000

Another thing that might help is setting the run port in the environment variables like this

export FLASK_RUN_PORT=8000

Let me know if either of these work.

port management in python/flask application

This code test.py doesn't change port that's specified in .run() args:

from flask import Flask    

app = Flask(__name__)

@app.route("/")
def index():
return "123"

app.run(host="0.0.0.0", port=8080) # or host=127.0.0.1, depends on your needs

There is nothing that can force flask to bind to another TCP port in allowed range if you specified the desired port in run function. If this port is already used by another app - you will see
OSError: [Errno 98] Address already in use
after launching.

UPD: This is output from my pc if I run this code several time using python test.py command:

artem@artem:~/Development/$ python test.py
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development$ python test.py
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development/$ python test.py
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
^Cartem@artem:~/Development/$ python test.py
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Nov/2017 17:04:56] "GET / HTTP/1.1" 200 -

As you can see flask gets binded to 8080 port every time.

UPD2: when you will setup production env for your service - you won't need to take care of ports in flask code - you will just need to specify desired port in web server config that will work with your scripts through wsgi layer.



Related Topics



Leave a reply



Submit