Flask server port change

Question:
I have a flask server and it is running on port 127.0.0.1 With the code below. How do I change it to 0.0.0.0 for deployment?

Repl link:
https://replit.com/@AndrewDeng3/Gizmo-Discord-Bot

from flask import Flask
from threading import Thread

app = Flask('')

@app.route('/')
def home():
    return '<!DOCTYPE html><html><head><meta http-equiv="refresh" content="5;url=https://gizmo-ai-discord-bot.mileswk.repl.co/"></head><body><h1>Redirecting...</h1></body></html>'

def run():
  app.run(host='0.0.0.0',port=8080)

def keep_alive():  
    t = Thread(target=run)
    t.start()

You don’t have to run a Flask server to deploy a bot. Instead, you should deploy your bot to a reserved VM and remove the Flask server startup

2 Likes

If you want to run this, change line 4 to:

app = Flask('app')

At lines 10-11, remove def run().
Then, at the last line of the program, insert line 11 there, and remove all indents.
Final:

from flask import Flask
from threading import Thread

app = Flask('app')

@app.route('/')
def home():
    return '<!DOCTYPE html><html><head><meta http-equiv="refresh" content="5;url=https://gizmo-ai-discord-bot.mileswk.repl.co/"></head><body><h1>Redirecting...</h1></body></html>'

def keep_alive():  
    t = Thread(target=run)
    t.start()

app.run(host='0.0.0.0', port=8080)

Hope this helps!

That is not necessary. anything can be in the string AFAIK.

What they’re trying to do is run a Flask app and Discord bot at the same time, so they’re using threading.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.