繁体   English   中英

500 内部服务器错误 - GCP App Engine

[英]500 Internal Server Error - GCP App Engine

我在 GCP App Engine 中玩耍并尝试使用 python 连接到 MySQL 数据库,然后返回/打印特定表的第一行,以便在单击应用程序链接时在浏览器中显示如下内容:


sub_event_id = 1234567

事件类型 = 1

date_time = 2019-09-22 00:00:00

ip = 未知

property_id = 1

联系人 ID = 1234567

address_id = 1234567


以下是我正在使用的代码以及部署应用程序后出现的错误。 谁能告诉我我做错了什么? 谢谢。

这是我的 main.py 文件中的代码:

from flask import Flask
import sqlalchemy as sa
app = Flask(__name__)
@app.route('/')
def dbresponse():
  engine = sa.create_engine('''mysql+pymysql://{username}:
                             {password}@{host}:{port}/{db_name}''')
      with engine.connect() as conn:
          # Execute the query and fetch all results
          response = conn.execute(
              "SELECT * FROM sub_events "
              "LIMIT 1"
          ).fetchall()
          # Print results
          for row in response:
              print("sub_event_id = ", row[0], )
              print("event_type   = ", row[1])
              print("date_time    = ", row[2])
              print("ip           = ", row[3])
              print("property_id  = ", row[4])
              print("contact_id   = ", row[5])
              print("address_id   = ", row[6], "\n")
      engine.dispose()


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

这是我的 app.yaml 文件中的代码:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3

这是我的 requirements.txt 文件中的代码:

Flask==1.1.1
gunicorn==19.9.0
SQLAlchemy==1.3.8
PyMySQL==0.9.3

运行gcloud app deploygcloud app browse然后单击生成的链接...

我收到 500 内部服务器错误:

500 Internal Server Error 服务器遇到内部错误,无法完成您的请求。 服务器过载或应用程序出错。

在这个 for 循环中有一个 integer 索引会带来 IndexError 的高风险。

在打印之前,我会首先检查行是否具有此大小。

有两种方法可以做到这一点(使用 try 或检查 len):

      for row in response:
          try:
              print("sub_event_id = ", row[0], )
              print("event_type   = ", row[1])
              print("date_time    = ", row[2])
              print("ip           = ", row[3])
              print("property_id  = ", row[4])
              print("contact_id   = ", row[5])
              print("address_id   = ", row[6], "\n")
          except IndexError:
              print("There is not complete data to print.")

或者

        for row in response:
            if len(row) >= 7: 
                print("sub_event_id = ", row[0], )
                print("event_type   = ", row[1])
                print("date_time    = ", row[2])
                print("ip           = ", row[3])
                print("property_id  = ", row[4])
                print("contact_id   = ", row[5])
                print("address_id   = ", row[6], "\n")
            else:
                print("There is not complete data to print.")

正如@jkleinne 指出的那样,还建议配置日志。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM