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

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

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
Copy link
Contributor

Seems like this should be closed based on above comments?

@davidism
Copy link
Member

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

@ralex2304
Copy link

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

@ivanleoncz
Copy link

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
@devnet1985
Copy link

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
Copy link

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

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

No branches or pull requests

6 participants