Flask after Server changes wont work correctly

83 views Asked by At

After I edited the server system, my flask application doesn't work correctly anymore.

File-structure:

├── fcgi-bin
|   └── hubb.fcgi
├── hubb
│   ├── cms.py
│   ├── dbconnect.py
│   ├── hubb.py
│   ├── static
|   │   └── ...
|   └── templates
|       └── ...
└── hubb.tekkkz.com
    └── .htaccess

Now my problem is, that if i use redirect(url_for(...)) it raises an internal server error, but when I use redirect without url_for, so with the real URL, it works. Check out yourself at hubb.tekkkz.com

My hubb.fcgi:

#!/usr/bin/env python2.7

import sys
sys.path.insert(0, "/var/www/virtual/tekkkz/hubb")

from flup.server.fcgi import WSGIServer
from hubb import app

if __name__ == '__main__':
    WSGIServer(app).run()

My hubb.py:

#!/usr/bin/python2.7

from flask import Flask, render_template, flash, request, url_for, redirect, session
from passlib.hash import sha256_crypt
from MySQLdb import escape_string as thwart
import gc
from functools import wraps

import cms
import dbconnect

app = Flask(__name__)

def login_required(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        if 'logged_in' in session:
            return f(*args, **kwargs)
        else:
            flash("You need to be logged in!")
            return redirect(url_for('login'))
    return wrap

def mod_required(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        if 'rank' in session:
            if session['rank'] < 5:
                flash("You need to be at least a Moderator to perform this action!")
                return redirect(url_for('main'))
            else:
                return f(*args, **kwargs)
        else:
            flash("You need to be logged in!")
            return redirect(url_for('login'))
    return wrap

def reload_session(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        if 'logged_in' in session:
            ret = 0
            c, conn = dbconnect.conn()
            data = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(session['username'])))
            if int(data) == 0:
                ret = 1
                flash("Error! Please send a report at [email protected]!")
            else:
                data = c.fetchone()
                session['logged_in'] = True
                session['uid'] = data[0]
                session['username'] = data[1]
                session['mail'] = data[3]
                session['credits'] = data[4]
                session['settings'] = data[5]
                session['rank'] = data[6]
                session['items'] = data[7]
            c.close()
            conn.close()
            gc.collect()
            if ret:
                redirect(url_for('main'))
        return f(*args, **kwargs)
    return wrap

@app.route('/')
@reload_session
def main():
    return render_template("main.html", nav=0, list=cms.main())

@app.route('/dashboard/')
@reload_session
@login_required
def dashboard():
    c, conn = dbconnect.conn()
    users = c.execute("SELECT * FROM users")
    users = c.fetchall()
    c.close()
    conn.close()
    gc.collect()
    return render_template("dashboard.html", nav=1, users=users, rank=cms.rank(), list=cms.dashboard())

@app.route('/users/')
@reload_session
def users():
    if 'uid' in request.args:
        ret = 0
        user = request.args['uid']
        c, conn = dbconnect.conn()
        data = c.execute("SELECT * FROM users WHERE uid = (%s)", (thwart(str(user))))
        if int(data) == 0:
            flash("Invalid UserID. This user doesn't exist!")
        else:
            data = c.fetchone()
            ret = 1
        c.close()
        conn.close()
        gc.collect()
        if ret:
            return render_template("users.html", nav=2, user=data)
        return redirect(url_for('main'))
    elif 'user' in request.args:
        ret = 0
        user = request.args['user']
        c, conn = dbconnect.conn()
        data = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(user)))
        if int(data) == 0:
            flash("Invalid Username. This user doesn't exist!")
        else:
            data = c.fetchone()
            ret = 1
        c.close()
        conn.close()
        gc.collect()
        if ret:
            return render_template("users.html", nav=2, user=data)
        return redirect(url_for('main'))
    elif 'logged_in' in session:
        ret = 0
        user = session['uid']
        c, conn = dbconnect.conn()
        data = c.execute("SELECT * FROM users WHERE uid = (%s)", (thwart(str(user))))
        if int(data) == 0:
            flash("Error! Please send a report at [email protected]!")
        else:
            data = c.fetchone()
            ret = 1
        c.close()
        conn.close()
        gc.collect()
        if ret:
            flash("To view the profile of other users please use the User Search Bar!")
            return render_template("users.html", nav=2, user=data)
    else:
        flash("You need to be logged in to see your own profile!")
    return redirect(url_for('login'))

@app.route('/users/search/', methods=['GET','POST'])
@reload_session
def search():
    if request.method == "POST":
        username = request.form['username']
        if username.isdigit():
            return redirect("/users/?uid="+username)
        else:
            return redirect("/users/?user="+username)
    return redirect(url_for('main'))

@app.route('/users/edit/', methods=['GET','POST'])
@reload_session
@mod_required
def edituser():
    if 'uid' in request.args:
        ret = 0
        user = request.args['uid']
        c, conn = dbconnect.conn()
        data = c.execute("SELECT * FROM users WHERE uid = (%s)", (thwart(str(user))))
        if int(data) == 0:
            flash("Invalid UserID. This user doesn't exist!")
        else:
            data = c.fetchone()
        if session['rank'] > data[6] or session['rank'] == 7:
            if 'c' in request.args and request.method == "POST":
                if request.form['rank'] >= session['rank'] and session['rank'] != 7:
                    flash('You have not enought permissions to set the rank of this user higher or equals to your own!')
                else:
                    data = c.execute("UPDATE users SET username=(%s), mail=(%s), credits=(%s), rank=(%s), items=(%s)  WHERE uid = (%s)",
                    (thwart(request.form['username']), thwart(request.form['mail']), thwart(str(request.form['credits'])), thwart(str(request.form['rank'])), thwart(request.form['items']), (thwart(str(user)))))
                    if int(data) == 0:
                        flash("Error at editing this user!")
                    else:
                        flash("successfully edited '"+user+"'!")
            else:
                ret = 1
        else:
            flash('You have not enought permissions to edit this user!')
        c.close()
        conn.close()
        gc.collect()
        if ret:
            return render_template("edit.html", nav=2, user=data)
    return redirect(url_for('dashboard'))

@app.route('/users/delete/')
@reload_session
@mod_required
def deluser():
    if 'uid' in request.args:
        user = request.args['uid']
        c, conn = dbconnect.conn()
        data = c.execute("SELECT * FROM users WHERE uid = (%s)", (thwart(str(user))))
        if int(data) == 0:
            flash("Invalid UserID. This user doesn't exist!")
        else:
            data = c.fetchone()
        if session['rank'] > data[6] or session['rank'] == 7:
            data = c.execute("DELETE FROM users WHERE uid = (%s)", (thwart(str(user))))
            if int(data) == 0:
                flash("Invalid UserID. This user doesn't exist!")
            else:
                flash("successfully deleted '"+user+"'!")
        else:
            flash('You have not enought permissions to delete this user!')
        c.close()
        conn.close()
        gc.collect()
    return redirect(url_for('dashboard'))

@app.route('/billing/shop/')
@reload_session
def shop():
    return render_template("billing.html", nav=3, list=cms.billing())

@app.route('/billing/credits/')
@reload_session
def credits():
    return render_template("billing.html", nav=4, list=cms.billing())

@app.route('/billing/history/')
@reload_session
def history():
    return render_template("billing.html", nav=5, list=cms.billing())

@app.route('/support-donate/')
@reload_session
def support():
    return render_template("support-donate.html", nav=6)

@app.route('/login/', methods=['GET','POST'])
@reload_session
def login():
    if request.method == "POST":
        ret = 0
        username = request.form['username']
        password = request.form['password']
        c, conn = dbconnect.conn()
        data = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
        if int(data) == 0:
            flash("Invalid Username. Please try again!")
        else:
            data = c.fetchone()
            if sha256_crypt.verify(password, data[2]):
                session['logged_in'] = True
                session['uid'] = data[0]
                session['username'] = data[1]
                session['mail'] = data[3]
                session['credits'] = data[4]
                session['settings'] = data[5]
                session['rank'] = data[6]
                session['items'] = data[7]
                flash("You are now logged in!")
                ret = 1
            else:
                flash("Invalid Password. Please try again!")
        c.close()
        conn.close()
        gc.collect()
        if ret:
            return redirect(url_for('dashboard'))
    return render_template("login.html", nav=7)

@app.route('/logout/')
@reload_session
@login_required
def logout():
    session.clear()
    flash("You have been logged out!")
    gc.collect()
    return redirect(url_for('main'))

@app.route('/register/', methods=['GET','POST'])
@reload_session
def register():
    if request.method == 'POST':
        ret = 0
        username = request.form['username']
        mail = request.form['mail']
        password = sha256_crypt.encrypt((str(request.form['password'])))
        c, conn = dbconnect.conn()
        x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
        if int(x) > 0:
            flash("This username is already taken. Please choose another!")
        else:
            c.execute("INSERT INTO users (username, password, mail, credits, settings, rank, items) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                (thwart(username), thwart(password), thwart(mail), thwart(str(100)), thwart(""), thwart(str(1)), thwart("")))
            conn.commit()
            flash("You are successfully registered!")
            data = c.execute("SELECT * FROM users WHERE username = (%s)", (username))
            data = c.fetchone()
            session['logged_in'] = True
            session['uid'] = data[0]
            session['username'] = data[1]
            session['mail'] = data[3]
            session['credits'] = data[4]
            session['settings'] = data[5]
            session['rank'] = data[6]
            session['items'] = data[7]
            ret = 1
        c.close()
        conn.close()
        gc.collect()
        if ret:
            return redirect(url_for('dashboard'))
    return render_template("register.html", nav=8)

@app.errorhandler(404)
@reload_session
def error_404(error):
    return render_template('404.html', nav=0), 404

if __name__ == "__main__":
    app.secret_key = 'ae58c691-a088-4821-acf9-a9f73b38864d'
    app.run(debug=True, port=61125)

And my .htaccess:

RewriteEngine On
RewriteRule ^(.*)$ /fcgi-bin/hubb.fcgi/$1 [QSA,L]

So why is there an internal server error?

0

There are 0 answers