我爲每個執行查詢的函數添加了一個db.session.close()。查看錯誤還顯示其他人有這個問題,因爲與S3失去聯繫。在本地它工作得很好。但是運行相同與mod_wsgi的Ubuntu的服務器上的Apache2顯示以下錯誤:Boto3顯示ConnectionReset錯誤
185.27.213.237 - - [17/Sep/2017 16:31:34] "GET/HTTP/1.1" 200 -
185.27.213.237 - - [17/Sep/2017 16:31:39] "POST/HTTP/1.1" 302 -
('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
185.27.213.237 - - [17/Sep/2017 16:31:50] "GET /space/user HTTP/1.1" 200 -
從Apache日誌:
[wsgi:error] [pid 1456:tid 139792612300544] ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
第一行是我打開登錄路由。使用正確的憑據登錄後,將顯示302錯誤。在下面我張貼我的登錄路線和成功登錄後重定向到的網站。我使用sqlite3,因爲我只有幾個用戶。
#Login
@app.route('/', methods=['GET', 'POST'])
def index():
form = LoginForm()
try:
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user:
if bcrypt.check_password_hash(user.password, form.password.data):
login_user(user, remember=False)
return redirect(url_for('showspace', spacename=user.username))
return render_template('login.html', form=form, ermsg="Invalid credentials")
return render_template('login.html', form=form)
except Exception as ermsg:
db.session.rollback()
print(ermsg)
return redirect(url_for('index'))
finally:
db.session.close()
#Dashboard new
@app.route('/space/<spacename>', methods=['GET', 'POST'])
@login_required
def showspace(spacename):
try:
selectedspace=spacename
spacelist = Space.query.filter(Space.owner.any(id=current_user.id)).all()
hasaccess = User.query.join(User.spaces).filter(User.username==current_user.username, Space.name==selectedspace).first()
if hasaccess != None:
conn = boto3.resource('s3')
mybucket = conn.Bucket(selectedspace)
return render_template('dashboard.html', spaces=spacelist, filelist=mybucket.objects.all(), name=current_user.username, selectedspace=selectedspace)
else:
return "You don't have permission to view this space!"
except:
db.session.rollback()
return 'Something went wrong'
finally:
db.session.close()
數據庫連接將不會被關閉,因爲finally因爲前面的'return'而沒有運行。 –
我認爲這與Boto3有關。我已經編輯了我的問題。知道最後甚至沒有被執行,這仍然是件好事。我將在每個try塊的末尾添加db.session.close()。謝謝。 – user3080315
@KlausD .:那是不正確的。無論「返回」,「finally」塊都會運行。 [這裏是文檔。](https://docs.python.org/2/tutorial/errors.html#defining-clean-up-actions) –