Dawson
Dawson

Reputation: 4403

Flask SERVER_NAME never correct

I am currently working on a Flask application hosted on Amazon's AWS. The application I am working on has several components, of which I am working on two. I am looking to setup subdomains for these components though am having some trouble.

I want :

The www subdomain is a static site and everything with that is going fine. I was able to setup Route53 and GoDaddy so that dashboard.example.com and api.example.com are hitting my EC2 server running my Flask application. I have confirmed this because I can see the request (which is 404ing) when I hit the domain with my browser.

The problem I am having is that Flask expects the app.config.SERVER_NAME to be set to the server name, which must also match URI. The server name of my EC2 instance is ec2-###-###-###-###.compute-1.amazonaws.com.

If I set my server name to this, the Flask application runs fine, and I get a 404 page, and see the request come by watching the output of the Flask application when I GET to either dashboard.example.com or api.example.com.

To not display the 404 page app.config.SERVER_NAME should be set to example.com, though if I do this the Flask application will not run, and I get socket.error: [Errno 99] Cannot assign requested address.

Here is my code :


from flask import \
    Flask, \
    request

# my imports
from dashboard import blueprint as dashboard

app = Flask(__name__)

@app.route('/')
def catch():
    return request.url

def main():
    print('Starting main ...')

    app.register_blueprint(dashboard)

if __name__ == '__main__':
    main()

app.config['SERVER_NAME'] = 'example.com'
app.run(
    host=app.config['server_name'],
    port=80,
    debug=True,
)

And here is my dashboard module :


from flask import \
    render_template, \
    Blueprint,\
    Response

blueprint = Blueprint(
    'dashboard', __name__,
    static_folder='static',
    template_folder='templates',
    subdomain='dashboard',)

@blueprint.route('/', methods=['GET'])
def _():
    return render_template('index.html')

@blueprint.route('/js/')
def _js(path):
    print('js : %s' % path)
    with blueprint.open_resource('static/js/' + path, mode='r') as f:
        content = f.read()
        return Response(content, mimetype='text/javascript')

@blueprint.route('/css/')
def _css(path):
    print('css : %s' % path)
    with blueprint.open_resource('static/css/' + path, mode='r') as f:
        content = f.read()
        return Response(content, mimetype='text/css')

Thank you all in advance for any help provided.

Upvotes: 2

Views: 10596

Answers (1)

Joe Doherty
Joe Doherty

Reputation: 3948

I did comment but I managed to reproduce the error exactly in test.

You can not bind to the address example.com and the IP address is not listed on the local machine. I can bind to mydomain.com because it resolves to the address on my server.

Also I think there is a typo in the example i take it you mean host=app.config['SERVER_NAME'] and not have the app missing or the SERVER_NAME in lowercase?

Joe

Upvotes: 1

Related Questions