简体   繁体   English

Boto3显示ConnectionReset错误

[英]Boto3 shows ConnectionReset Error

I added a db.session.close() to every function that does a query. 我向执行查询的每个函数添加了db.session.close()。 Looking up the error also showed me that others had this issue because of the lost connection to S3. 查找错误还向我表明,由于与S3的连接断开,其他人也遇到了此问题。 Locally it works pretty well. 在本地,效果很好。 However running the same on a Ubuntu server with mod_wsgi and Apache2 shows the following error: 但是,在具有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 -

From the Apache logs: 从Apache日志中:

[wsgi:error] [pid 1456:tid 139792612300544] ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

The first line was me opening the login route. 第一行是我打开登录路径。 After logging in with the correct credentials it shows the 302 error. 使用正确的凭据登录后,它显示302错误。 In the following I post my login route and the site that is redirected to after a successful login. 在下面的文章中,我发布了我的登录路线以及成功登录后重定向到的站点。 I'm using sqlite3 since I only have a couple of user. 我正在使用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()

It would be so much cleaner if you write a decorator and execute your methods inside a database session. 如果您编写一个装饰器并在数据库会话中执行您的方法,它将更加干净。 Like so 像这样

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