繁体   English   中英

Boto3显示ConnectionReset错误

[英]Boto3 shows ConnectionReset Error

我向执行查询的每个函数添加了db.session.close()。 查找错误还向我表明,由于与S3的连接断开,其他人也遇到了此问题。 在本地,效果很好。 但是,在具有mod_wsgi和Apache2的Ubuntu服务器上运行相同命令会显示以下错误:

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()

如果您编写一个装饰器并在数据库会话中执行您的方法,它将更加干净。 像这样

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

暂无
暂无

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

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