flask icon indicating copy to clipboard operation
flask copied to clipboard

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

Open MartijnRas opened this issue 9 years ago • 8 comments

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

MartijnRas avatar Dec 14 '16 12:12 MartijnRas

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.

davidism avatar Jan 16 '17 22:01 davidism

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.

davidism avatar Jan 16 '17 23:01 davidism

Seems like this should be closed based on above comments?

jeffwidman avatar May 19 '17 19:05 jeffwidman

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

davidism avatar May 19 '17 19:05 davidism

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

ralex2304 avatar Jan 17 '18 16:01 ralex2304

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()

ivanleoncz avatar Aug 12 '18 02:08 ivanleoncz

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

devnet1985 avatar Sep 14 '20 11:09 devnet1985

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')

devnet1985 avatar Sep 14 '20 11:09 devnet1985