502 Bad Gateway Error when adding mysql.connector in my Flask application

310 Views Asked by At

When I run my flask application on my local machine I am able to execute queries successfully from my sql database in google cloud. However, when I deploy my app onto the google cloud platform, it throws me a "502 Bad Gateway" error once it successfully builds.

Main.py file below:

imports

from flask import Flask, render_template, request
import mysql.connector
from mysql.connector.constants import ClientFlag

configuration/setup

config = {
    'user': 'user',
    'password': 'passhere!',
    'host': 'hosthere',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': 'ssl/file',
    'ssl_cert': 'ssl/file',
    'ssl_key': 'ssl/file',
    'database': 'db'
}

cnxn = mysql.connector.connect(**config)
app = Flask(__name__, template_folder="templates")

app route

@app.route("/page/", methods=['GET', 'POST'])
def page():
    if request.method == 'GET':
        return render_template('/page.html')
    elif request.method == 'POST':
        pick = request.form["pickzip"]
        pick = pick[:2]
        dest = request.form["destzip"]
        dest = dest[:2]
        ########################### SQL COMMAND GRABS LANE ####################################
        cursor = cnxn.cursor()
        cursor.execute("""SQL COMMAND""", (pick,dest,))
        data = cursor.fetchall()
        if data:
            headings = ("Carrier", "Actual", "Estimated", "Load Count")
            return render_template('/page.html',headings=headings, data=data)
        else:
            data = (["NULL", "NULL", "NULL", "NULL"])
            headings = ("Carrier", "Actual", "Estimated", "Load Count")
            return render_template('/page.html', headings=headings, data=data)
1

There are 1 best solutions below

3
Ray Terrill On

It sounds like you need to adjust your flask app config to listen on port 8080. Have a look at this article: https://cloud.google.com/appengine/docs/standard/python3/building-app. I believe the default port for flask is 5000.