2

I have a server with a domain, for example https://my.site.com. But when I start Flask, then the domain doesn't work when I connect. What's the problem?

Code:

from flask import Flask, jsonify
import os

ASSETS_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__)

@app.route('/')
def index():
    return 'Flask is running!'

if __name__ == '__main__':
    context = ('server.crt', 'server.key')
    app.run(debug=True, ssl_context=context, port=3030)

and log:

 * Running on https://127.0.0.1:3030/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 284-971-705

I connect to https://my.site.com:3030 in browser, and have 'forever load'

Rick Petrev
  • 133
  • 1
  • 5

2 Answers2

6

To address the question itself, the log indicates that your Flask application is listening on the IP 127.0.0.1 (localhost):

Running on https://127.0.0.1:3030/

This way Flask will only accept connections made to this address only (127.0.0.1). Instead, you need to start Flask telling it to listen for new connection on the public IP of your server. Like this:

flask run --host=PUBLIC_IP

or, alternatively, simply listening on all IPs:

flask run --host=0.0.0.0

(where 0.0.0.0 means "all IPs available to the operating system")

See also: https://flask.palletsprojects.com/en/1.1.x/quickstart/#public-server


Flask in production

Based on the original log from the question, it appears that there is a couple of other matters that are better to be addressed before going to production. Making a server public (as per above) while ignoring these items is dangerous from a security perspective and the danger is very real. Specifically with a registered DNS, which, besides its original purpose, also acts as an advertisement for every bot out there. As the subject in itself is quite big, only major items are outlined below:

Minimal:

  • Disable debug. Debugger allows remote code execution and serving "public" requests by a server with a debugger enabled is a big (huge) and very real security risk. See more about debug mode in Flask.

  • Generate and configure a server secret. Server secret is used, for example, to sign cookies, and it needs to be unique and, well, secret. See more about server secret.

  • Run a WSGI server instead of a Flask development server. When you start flask run it runs a development server which is not designed for performance or reliability. However, applications implemented with Flask framework are valid WSGI applications, and can be served by any WSGI server software, such as uWSGI for example, or gunicorn, or waitress to name a few popular ones.

Normally also:

  • Run Flask server behind a reverse proxy, such as nginx for example. Reverse proxy may handle a bunch of functionality, such as serving static files, provide SSL termination (https), compression, caching, etc. in a very effective (in terms of performance) manner.

  • Consider persisting Flask application log. In case of any troubles you might want to understand what happened. By default Flask logs are directed to the stdout and won't be persisted should the server restart. (So all past logs will be lost).

The check-list of going to production certainly goes on and on, but I think above are the items that must be addressed in order to avoid a serious trouble.


Alternatively you may consider other options that take care of most of the typical "production" configuration. Options below may be a good idea because this way you will rely on existing and well-supported configuration made by experts. For example:

  • Deploy to a managed service, such as AWS ElasticBeanstalk, Heroku, etc. etc.
  • If you prefer having more control over the environment, consider packaging the application into a Docker image based on some well-supported base image with some of the above items pre-configured (for instance: https://github.com/tiangolo/uwsgi-nginx-flask-docker)

See also:

Tim
  • 12,318
  • 7
  • 50
  • 72
0

I am going to assume that your Flask instance is still running locally. To test this theory, try localhost:3030 or 127.0.0.1:3030 in your browser and see if your project loads there without issue.

To go from testing to deployment, you will want to use some additional infrastructure to host your project. It will not be as simple as just trying to run it from your IDE. Probably the most obvious starting point is to use PythonAnywhere, which will allow you to host a Flask instance with a free account. Their documentation is solid: https://help.pythonanywhere.com/pages/Flask/

From memory, I don't think their free tier allows you to use a custom domain, meaning you would need to upgrade to a paid account to about $5 per month or higher.

If you want something more rigorous or are considering how to progress, you might also consider throwing your Flask project into a GitHub repository and then signing up for Microsoft Azure. You can then create a webapp within Azure and point it to your GitHub repository. This will be more expensive but scales to whatever you need and will allow use of a custom domain.