Flask server setup for one REPL works, but the same code in a new REPL doesn't?

Question:
I’m making a flask server on a DiscordBot Repl, but for some reason after it loads the html page, down in the bottom right of console, I see the notification “Reconnecting” “Connected” pop up and that causes the Flask server to stop displaying the HTML page. I have the same code in an older DiscordBot Repl, but I don’t run into any issues displaying the html page and don’t get the “Reconnecting” “Connecting” notification.
Repl link/Link to where the bug appears:
https://replit.com/@LyndenSylver/Nopenheimer#keep_alive.py
Screenshots, links, or other helpful context:
Here’s the older REPL that has the same Flask server setup except it has more app routes and some database connections:
https://replit.com/@LyndenSylver/DiscordBot#keep_alive.py
DiscordBot REPL working code:

from flask import Flask, render_template, request
from threading import Thread
import sqlite3
import sys

app = Flask(__name__)


con = sqlite3.connect('tinker.db')
print("Opened database successfully")

con.execute("""CREATE TABLE IF NOT EXISTS users (
 id INTEGER PRIMARY KEY AUTOINCREMENT,
 user TEXT,
 user_id INTEGER UNIQUE);""")
print("Table created successfully")
con.close()

@app.route('/')
def home():
    return render_template('user.html')

@app.route('/webhook', methods = ['POST'])
def webhook():
   print("Recieved Webhook")
   sys.stdout.flush()
   if request.method == 'POST':
     print(request.json)
     return 'Success', 200
   else:
     abort(400)

@app.route('/newuser')
def new_user():
    return render_template('home.html')

@app.route('/stats')
def stats():
   return render_template('stats.html')

@app.route('/addrec', methods = ['POST','GET'])
def addrec():
     if request.method == 'POST':
       try:
         dn = request.form['dn']
         di = request.form['di']

         with sqlite3.connect("tinker.db") as con:
             cur = con.cursor()
             cur.execute("INSERT INTO users (user, user_id) VALUES (?,?)",(dn, di) )
            
             con.commit()
             msg = "Record successfully added"
             print(msg)
       except:
          con.rollback()
          msg = "error in insert operation"
      
       finally:
         if di == '425477636333240331':
           con.close()
           return render_template("result.html", msg = msg)
          
         if ((not dn) or (not di)):
           msg = "Please fill the required fields"

           return render_template("user.html", msg = msg)

         else:
           con.close()
           return render_template("thx.html", msg = msg)
     else:
       return render_template("user.html")

@app.route('/list')
def list():
    con = sqlite3.connect("tinker.db")
    con.row_factory = sqlite3.Row
   
    cur = con.cursor()
    cur.execute("select * from users")
   
    rows = cur.fetchall(); 
    return render_template("list.html",rows = rows)

@app.route('/doc')
def help_list():
    return render_template('documentation/docs.html')
    print("Page under ")

@app.route('/rps')
def rps():
    return render_template('documentation/rps.html')

@app.route('/support')
def support():
    return render_template('documentation/support.html')

@app.route('/donate')
def donate():
    return render_template('documentation/donate.html')

@app.route('/don')
def don():
    return render_template('documentation/don.html')

@app.route('/work')
def work():
    return render_template('documentation/work.html')

@app.route('/vote')
def vote():
    return render_template('documentation/vote.html')

@app.route('/youtube')
def youtube():
    return render_template('documentation/youtube.html')


if __name__ == "__main__":
   app.run(debug=True)

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

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

Nopenheimer REPL not working code:

from flask import Flask, render_template
from threading import Thread

app = Flask(__name__)

@app.route('/')
def home():
  return render_template('home.html')


if __name__ == "__main__":
  app.run(debug=True)

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

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

Ofc, it’s entirely possible the error is somewhere else, but… I genuinely don’t know what’s causing this behavior and I’m wondering if it may be an environment issue
where something is different that I can’t see.

Hey @LyndenSylver!

Try changing:

to:

if __name__ == "__main__":
  app.run()
1 Like

I suspect the weird replit.dev URL is the culprit, but I have no way to check that myself.

As that’s the one major difference between the old Repl and Newer REPLs, my old one used the old URL format, but the new one uses the replit.dev format

I tried that, but it made no difference. I’m wondering if it’s some weird bug that only exists in newer repls (post- AlwaysOn Removal, and change to deployments and replit.dev URLS)

Because it seems my repl that existed before those changes works perfectly fine, but even forking my code from that old REPL to a new one, doesn’t work with exactly identical code.

I’m sorry, I should have read your original post more. A disconnect-reconnect loop shouldn’t actually be tied to the Repl itself, could you share network logs of the tab that has your Repl open? I’m suspecting some critical domain is getting blocked, causing the issue.

2 Likes

I have the .har file, but it’s not among the supported formats for uploading, but here’s screenshots of the network log:




Does the Console have any errors?
Also, try visiting replit.dev. Can you see the Replit page about not running, or is it blocked by any firewalls/ISPs?

Just took a look at the .har file using Google Admin Toolbox Har Analyzer, and it looks like the style.css file throws a 502 error, and then the replit.dev URL also throws 2 502 errors.

Here’s the style.css:


body {
  background-color: #73d2a0;
  color: #000000;
}

.btn {
  display: inline;
  padding: 15px 25px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  background-color: #d30f0f;
  color: #ffffff;
  outline: none;

  border: none;
  border-radius: 15px;
  box-shadow: 0 9px #999;
}

.btn:hover {background-color: #757575}

.btn:active {
  background-color: #360000;
  box-shadow: 0 5px #007e30;
  transform: translateY(4px);
}

.required { 
  color: #690000; 
}

small {
  font-size: 16px;
}

The console throws no errors, and the webview has the ASCII computer, keyboard, and mouse and below it it says “Looks like you have a server running.” and the blue button saying “Show web content”. Clicking that button gets me the “Hmm… We couldn’t reach this Repl” “Make sure this Repl has a port open and is ready to receive HTTP traffic” page in the webview. I do have an open port on :8080. I don’t think anything is blocked by firewalls or ISPs

Does changing it to this work?

<link rel="stylesheet" href="/static/css/style.css ">

Then, in your keep_alive.py, try changing it to:

from flask import Flask, render_template
from threading import Thread

app = Flask(__name__)

@app.route('/')
def home():
  return render_template('home.html')

def run():
  app.run(host='0.0.0.0',port=8080)
def keep_alive():
  t = Thread(target=run)
  t.start()


if __name__ == "__main__":
  run()

I removed the app.run in the if statement, calling it later.
Try this, see if it works.

yes and no? It sorta works but resets to a blank white page after a couple seconds and then goes to the “Looks like you have a server running” page. It also doesn’t fix the reconnect-connect issue. With your tweaks it sometimes uses the stylesheet for the html, and other times doesn’t before setting to the white page

1 Like

I wonder… Could you try opening it in a new tab in the short time before it disconnects, and then don’t use the Webview tab, does the issue still occur?

It’s such a small window of time between when it loads up and when it starts the reconnect-connect loop (a matter of miliseconds) that the reconnect-connect loop starts when I open it in a new tab (which lands me on the “Hmm… We couldn’t reach this Repl” page)