예제 #1
0
파일: views.py 프로젝트: xzlmark/flask
def get_cache():
    addr = request.remote_addr  # 获取访问的IP
    key = addr + 'user'
    result = cache.get(key)
    # 判断,如果存在,则冲缓存中获取,如果不存在,则从数据库中获取
    if result:
        print(addr, '从缓存中加载数据')
        return result
    else:
        result = render_template('base.html')
        print(addr, '从数据库中加载数据,模拟')
        cache.set(key, result, timeout=30)
    return result
예제 #2
0
파일: views.py 프로젝트: xzlmark/flask
def before():
    print(request.remote_addr)
    print('before')

    # 通过useragent来进行反扒,useragent如果为空,则不允许访问,如requests.get不加请求头,则会报错。
    if not request.user_agent:
        abort(500)
    addr = request.remote_addr  # 获取访问的IP
    key = addr + 'before'
    value = cache.get(key)
    # 判断,如果存在,则冲缓存中获取,如果不存在,则从数据库中获取
    if value:
        return '小伙子,别爬了'
    else:
        cache.set(key, '么么哒', timeout=30)
예제 #3
0
    def post(self):
        args = parse_base.parse_args()

        password = args.get('password')

        action = args.get('action').lower()

        if action == USER_ACTION_REGISTER:
            args_register = parse_login.parse_args()
            username = args_register.get('username')
            phone = args_register.get('phone')

            movie_user = AdminUser()
            movie_user.username = username
            movie_user.password = password
            movie_user.phone = phone

            if not movie_user.save():
                abort(400)

            data = {
                "status": HTTP_CREATE_OK,
                "msg": "用户创建成功",
                "data": movie_user
            }
            return marshal(data, single_movie_user_fields)
        elif action == USER_ACTION_LOGIN:

            args_login = parse_login.parse_args()
            username = args_login.get("username")
            phone = args_login.get("phone")

            user = get_movie_user(username) or get_movie_user(phone)

            if not user:
                abort(400, msg="用户不存在")
            if not user.verify_password(password):
                abort(401, msg="用户名或密码错误")
            if user.is_delete:
                abort(401, msg="用户不存在")

            token = generate_movie_user_token()

            cache.set(token, user.id, timeout=60 * 60 * 24 * 7)
            data = {"status": HTTP_OK, "msg": "登陆成功", "data": token}
            return data
        else:
            abort(400, msg="请提供正确的参数")
예제 #4
0
def students():
    action = request.args.get("action")

    username = request.form.get("username")
    password = request.form.get("password")

    if action == "register":

        student = Student()
        student.s_name = username
        student.s_password = password

        if not student.save():
            abort(401)
        return "注册成功"

    elif action == "login":

        # Student.query.filter(Student.s_name.__eq__(username))
        student_list = Student.query.filter(Student.s_name == (username)).all()
        if not student_list:
            abort(404)
        student = student_list[0]

        if not student.verify_password(password):
            abort(401)

        token = uuid.uuid4().hex

        cache.set(token, student.id, timeout=60 * 60 * 24)

        data = {"msg": "登录成功", "token": token}

        return jsonify(data)

    else:
        abort(400)