因此,当我第一次部署我的应用程序时,它确实运行良好。它显示了所有页面,我能够从我的PythonAnywhere MySQL数据库进行写入和读取。然后10分钟后,我收到内部服务器错误。然后,当我重新加载应用程序时,它会工作10分钟,然后返回到内部服务器错误。下面的详细信息显示了我与数据库通信的代码,server.log错误代码以及我得到的error.log代码。
服务器日志:
2019-04-29 01:06:40 Mon Apr 29 01:06:40 2019 - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request / (ip 10.0.0.162) !!!
================================================ ==============================
错误日志:
2019-04-29 01:06:40,959: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/chronic007/TheCronica/index.py", line 21, in home_page
cursor.execute(query)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 393, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 586, in cmd_query
statement))
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 386, in _send_cmd
packet_number)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/network.py", line 104, in send_plain
raise errors.OperationalError(str(err))
OperationalError: [Errno 32] Broken pipe
================================================ =============================
这是我的python flask文件:
from flask import Flask
from flask import render_template
from flask import request
import mysql.connector
app = Flask(__name__)
db = mysql.connector.connect(
host="xxxx.mysql.pythonanywhere-services.com",
user="xxxxx",
passwd="xxxxxx",
database= "xxxxxx"
)
cursor = db.cursor()
@app.route('/')
def home_page():
query = "SELECT * FROM Articles"
cursor.execute(query)
data = cursor.fetchall()
return render_template('index.html', value=data)
@app.route('/static/index.html')
def static_home():
query = "SELECT * FROM Articles"
cursor.execute(query)
data = cursor.fetchall()
return render_template('index.html', value=data)
@app.route('/static/about.html')
def static_about():
return render_template('about.html')
@app.route('/admin')
def admin_page():
return render_template('admin.html')
@app.route('/admin', methods=['POST'])
def submit_form():
query = "INSERT INTO Articles (Title, Author, Date, Description, Content, Link) VALUES (%s, %s, %s, %s, %s, %s)"
if request.method == 'POST' :
title = request.form.get('Title')
author = request.form.get('Author')
date = request.form.get('Date')
description = request.form.get('Description')
content = request.form.get('Content')
link = request.form.get('Link')
val = (title, author, date, description, content, link)
cursor.execute(query, val)
db.commit()
return render_template('admin.html')
答案 0 :(得分:2)
您正在模块级(视图功能之外)打开数据库连接,因此在调用视图时该连接可能不再有效;数据库连接在闲置一段时间后会关闭,这可以解释您遇到的问题。您应该在每个使用它们的视图函数中创建db
和cursor
对象,甚至最好使用SQLAlchemy之类的数据库连接管理器。
This PythonAnywhere help page has more information about connection management和this blog post has a walkthough for creating a database-backed website using Flask and SQLAlchemy on PythonAnywhere。
答案 1 :(得分:0)
首先,此代码在if语句之外
val = (title, author, date, description, content, link)
cursor.execute(query, val)
db.commit()
您不需要两个视图即可处理管理员
@app.route('/admin', methods=['GET', 'POST'])
def submit_form():
query = "INSERT INTO Articles (Title, Author, Date, Description, Content, Link) VALUES (%s, %s, %s, %s, %s, %s)"
if request.method == 'POST' :
title = request.form.get('Title')
author = request.form.get('Author')
date = request.form.get('Date')
description = request.form.get('Description')
content = request.form.get('Content')
link = request.form.get('Link')
val = (title, author, date, description, content, link)
cursor.execute(query, val)
db.commit()
return render_template('admin.html', message="success")
else:
return render_template('admin.html')