Python Flask Bot Code

I’m writing discord bot with python 3 in glitch. I use flask to open the bot. server.py is my main file. The boot starts from there. I added the flask section in the main file to the text.

My problem is: if name == ‘__main __’: ‘Hello world’ is written on the page but the bot does not open. if name == ‘__server (main filename) __’: When you do, the page stays on ‘Starting …’ and the bot opens. What should I do to open the bot and have that page? My goal is to ask the page to open, I use UptimeRobot to make the bot 24/7.

import os
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(port=int(os.getenv('PORT', 5000)))

Using the if __name__ == '__main__' method is great for debugging a Flask application during development buts it’s not a great in production live app solution. For that your really want to use a WSGI Server ontop of Flask. The Flask documentation mentions this briefly here.

For this project I would use gunicorn because it’s incredibly simple to setup. Just add it to your dependencies or run pip(3) install gunicorn and set your start command to be gunicorn server:app. This will have gunicorn handle incoming requests and route them to your flask app. There’s nothing you have to do to your existing Flask app.