Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MartijnRas opened this issue Dec 14, 2016 · 6 comments

Comments

@MartijnRas
Copy link

commented Dec 14, 2016

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
@davidism

This comment has been minimized.

Copy link
Member

commented Jan 16, 2017

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

This comment has been minimized.

Copy link
Member

commented Jan 16, 2017

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.

@jeffwidman

This comment has been minimized.

Copy link
Member

commented May 19, 2017

Seems like this should be closed based on above comments?

@davidism

This comment has been minimized.

Copy link
Member

commented May 19, 2017

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

@davidism davidism closed this May 19, 2017

@ralex2304

This comment has been minimized.

Copy link

commented Jan 17, 2018

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

@ivanlmj

This comment has been minimized.

Copy link

commented Aug 12, 2018

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

@pallets pallets deleted a comment from mrshelly Jan 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.