Flask: app.run(host, port) and SERVER_NAME should correspond to where flask is actually running

Created on 14 Dec 2016  路  8Comments  路  Source: pallets/flask

Assuming the SERVER_NAME config variable is configured as '0.0.0.0:5555':

1) Calling app.run(host=None, port=None) results in flask running on http://127.0.0.1:5555/
2) Calling app.run(host='127.0.0.1', port=5000) results in flask running on http://127.0.0.1:5000/

In both cases the SERVER_NAME config variable does not correspond to where flask is actually running.

The host and port parameters in app.run() must of course take precedence, but should be set according to the SERVER_NAME config variable if present.

The current implementation in app.run():

if host is None:
    host = '127.0.0.1'
if port is None:
    server_name = self.config['SERVER_NAME']
    if server_name and ':' in server_name:
        port = int(server_name.rsplit(':', 1)[1])
    else:
        port = 5000

Could be replaced by the following:

_host = '127.0.0.1'
_port = 5000
servername = self.config['SERVER_NAME']
if server_name:
    if server_name and ':' in server_name:
        _host, _port = servername.split(':', 1)
if host is None:
    host = _host
if port is None:
    port = _port

The following can be used to make sure the SERVER_NAME config variable corresponds to where flask is actually running:

if servername:
    self.config['SERVER_NAME'] = host + ':' + port

Most helpful comment

Is there any way of putting the host and port variables in a separate file?

For example, defining a configuration file and having these variables inside of it, instead of defining these exactly on app.run():

app.config.from_pyfile('settings.cfg') 
app.run()

All 8 comments

0.0.0.0:5000 is not a valid server name. It's a valid bind, but that's not what SERVER_NAME is. Also, it's probably a bad sign when you're binding the dev server to the outside.

This won't (a cannot due to lazy loading) apply to the flask run command, which is the recommended way to run the dev server now.

Seems like this should be closed based on above comments?

Yes. Also, server name isn't necessarily the same as the local address the server is bound to.

How can I make 2 server names?
"example.com" and "www.example.com".

Is there any way of putting the host and port variables in a separate file?

For example, defining a configuration file and having these variables inside of it, instead of defining these exactly on app.run():

app.config.from_pyfile('settings.cfg') 
app.run()

How can I make 2 server names?
"example.com" and "www.example.com".

add www record in you DNS settings so when anyone type www.example.com it automatically redirect to your website

Assuming the SERVER_NAME config variable is configured as '0.0.0.0:5555':

  1. Calling app.run(host=None, port=None) results in flask running on http://127.0.0.1:5555/
  2. Calling app.run(host='127.0.0.1', port=5000) results in flask running on http://127.0.0.1:5000/

In both cases the SERVER_NAME config variable does not correspond to where flask is actually running.

The host and port parameters in app.run() must of course take precedence, but should be set according to the SERVER_NAME config variable if present.

The current implementation in app.run():

if host is None:
    host = '127.0.0.1'
if port is None:
    server_name = self.config['SERVER_NAME']
    if server_name and ':' in server_name:
        port = int(server_name.rsplit(':', 1)[1])
    else:
        port = 5000

Could be replaced by the following:

_host = '127.0.0.1'
_port = 5000
servername = self.config['SERVER_NAME']
if server_name:
    if server_name and ':' in server_name:
        _host, _port = servername.split(':', 1)
if host is None:
    host = _host
if port is None:
    port = _port

The following can be used to make sure the SERVER_NAME config variable corresponds to where flask is actually running:

if servername:
    self.config['SERVER_NAME'] = host + ':' + port

change port use flask run --host 0.0.0.0
or you can use
if __name__ == '__main__':
app.run(host='0.0.0.0')

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chuanconggao picture chuanconggao  路  4Comments

KeyC0de picture KeyC0de  路  4Comments

rochacbruno picture rochacbruno  路  3Comments

lnielsen picture lnielsen  路  3Comments

ghostbod99 picture ghostbod99  路  4Comments