-1

This might be a noob question, but I can't pinpoint what's going wrong with my code.

Below is a Flask backend for the Android app. Initially, I thought some error was taking place during JSON transfer but later realized the server wasn't actually running properly. Also noticed that the debug mode is off even though it is specified to run.

import flask
import mysql.connector
import sys
from flask import Flask
import json

app = Flask(__name__, template_folder="templates")


@app.route('/welcome')
def welcome():
    return '''
        <!doctype html>
        <title>Welcome</title>
        <h1>Welcome</h1>
        '''


@app.route('/', methods=['GET', 'POST'])
def chat():
    msg_received = flask.request.get_json()
    msg_subject = msg_received["subject"]

    if msg_subject == "register":
        return register(msg_received)
    elif msg_subject == "login":
        return login(msg_received)
    else:
        return "Invalid request."


def register(msg_received):
    firstname = msg_received["firstname"]
    lastname = msg_received["lastname"]
    username = msg_received["username"]
    password = msg_received["password"]

    select_query = "SELECT * FROM users where username = " + "'" + username + "'"
    db_cursor.execute(select_query)
    records = db_cursor.fetchall()
    if len(records) != 0:
        return "Another user used the username. Please chose another username."

    insert_query = "INSERT INTO users (first_name, last_name, username, password) VALUES (%s, %s, %s, MD5(%s))"
    insert_values = (firstname, lastname, username, password)
    try:
        db_cursor.execute(insert_query, insert_values)
        chat_db.commit()
        return "success"
    except Exception as e:
        print("Error while inserting the new record :", repr(e))
        return "failure"


def login(msg_received):
    username = msg_received["username"]
    password = msg_received["password"]

    select_query = "SELECT first_name, last_name FROM users where username = " + "'" + username + "' and password = " + "MD5('" + password + "')"
    db_cursor.execute(select_query)
    records = db_cursor.fetchall()

    if len(records) == 0:
        return "failure"
    else:
        return "success"


#def addloc(msg_received):


try:
    chat_db = mysql.connector.connect(host="localhost", user="root", passwd="password", database="chat_db")

except:
    sys.exit("Error connecting to the database. Please check your inputs.")
db_cursor = chat_db.cursor()

if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)

Any help would be appreciated.

davidism
  • 121,510
  • 29
  • 395
  • 339
  • app.run(port=6000) add this – Madan Raj Jun 19 '21 at 05:27
  • the port may be used for something else – Madan Raj Jun 19 '21 at 05:27
  • @MadanRaj changed port to 6000 like so `if __name__ == '__main__': app.run(host="0.0.0.0", port=6000, debug=True)` It was still running on the same port. `C:\Users\Nithil Sunilkumar\PycharmProjects\contactTracing\venv\Scripts\python.exe" -m flask run * Serving Flask app "app.py" * Environment: development * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)` – user9353487 Jun 19 '21 at 05:32
  • post the error.. – Madan Raj Jun 19 '21 at 08:57
  • @MadanRaj Error displayed in browser - Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application. I just found something, out of doubt I checked my previous flask project and came across the same error. I also tried creating a new flask project which ended up the same way even though it was just an auto generated hello world program. BTW I am doing this in pycharm – user9353487 Jun 19 '21 at 09:17

1 Answers1

0

That port might not be available. Just mention the port 8080 or 5000 or 6000 in the app.run

app.run(port = 8080, debug = True)
Amit Gupta
  • 2,698
  • 4
  • 24
  • 37
  • changed port to 6000 and 8080 like so `if __name__ == '__main__': app.run(host="0.0.0.0", port=6000, debug=True)` It was still running on the same port. `C:\Users\Nithil Sunilkumar\PycharmProjects\contactTracing\venv\Scripts\python.exe" -m flask run * Serving Flask app "app.py" * Environment: development * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)` – user9353487 Jun 19 '21 at 05:35
  • yes, it's running now. Open your browser and goto http://127.0.0.1:5000/ you should see your app – Amit Gupta Jun 19 '21 at 05:37
  • that page still shows internal error. I also have a /welcome page routed for displaying text which is showing not found. also debug mode is off, so I can't get any debug infos – user9353487 Jun 19 '21 at 06:06