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 · 8 comments

Comments

@MartijnRas
Copy link

@MartijnRas MartijnRas 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
Copy link
Member

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

Loading

@davidism
Copy link
Member

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

Loading

@jeffwidman
Copy link
Member

@jeffwidman jeffwidman commented May 19, 2017

Seems like this should be closed based on above comments?

Loading

@davidism
Copy link
Member

@davidism davidism commented May 19, 2017

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

Loading

@davidism davidism closed this May 19, 2017
@ralex2304
Copy link

@ralex2304 ralex2304 commented Jan 17, 2018

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

Loading

@ivanleoncz
Copy link

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

Loading

@pallets pallets deleted a comment from mrshelly Jan 24, 2019
@devnet1985
Copy link

@devnet1985 devnet1985 commented Sep 14, 2020

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

Loading

@devnet1985
Copy link

@devnet1985 devnet1985 commented Sep 14, 2020

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

Loading

@pallets pallets locked and limited conversation to collaborators Sep 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants