2017-09-17 34 views
0

我爲每個執行查詢的函數添加了一個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() 
+1

數據庫連接將不會被關閉,因爲finally因爲前面的'return'而沒有運行。 –

+0

我認爲這與Boto3有關。我已經編輯了我的問題。知道最後甚至沒有被執行,這仍然是件好事。我將在每個try塊的末尾添加db.session.close()。謝謝。 – user3080315

+0

@KlausD .:那是不正確的。無論「返回」,「finally」塊都會運行。 [這裏是文檔。](https://docs.python.org/2/tutorial/errors.html#defining-clean-up-actions) –

回答

0

如果您在數據庫會話中編寫裝飾器並執行您的方法,它將會非常乾淨。像這樣

def db_session(): 
    def wrapper(func): 
     def wrapped(*args, **kwargs): 
      # Executing the handler inside a db context 
      with Session as session: 
       try: 
        return func(session, *args, **kwargs) 
       except: 
        session.rollback() 

@app.route('/space/<spacename>', methods=['GET', 'POST']) 
@login_required 
@db_session() 
def showspace(session, spacename): 
    # your code 
+1

'finally'套件不會執行。從文檔:[「try語句的任何其他子句通過break,continue或return語句留下時,finally子句也會執行」。「](https://docs.python.org/ 2 /教程/ errors.html#限定-清理-動作)。 –

+0

謝謝。我正在使用Flask-SQLAlchemy。我認爲你發佈了香草SQLAlchemy。你能否將我轉換成Flask-SQLAlchemy以確保我不會導致更多問題。 – user3080315

+0

@ZachGates,是的,我編輯了我的答案。謝謝 – Juggernaut