Пример #1
0
import logging

from flask import Flask, request
from flask_cors import CORS
from flask_redis import FlaskRedis

import pika

from config import Config

logging.level = logging.DEBUG

redis_hashmap_key = os.environ.get("REDIS_HASHMAP_KEY")
rabbitmq_server = os.environ.get("RABBITMQ_SERVER")

redis_store = FlaskRedis()
pika_connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_server))


def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)

    CORS(app, resources={r"/*": {"origins": "*"}})

    redis_store.init_app(app)

    return app


def init_route(app):
Пример #2
0
import threading

from six import text_type
from flask import Flask
from flask_redis import FlaskRedis
from flask_ldapconn import LDAPConn

app = Flask("lavatar")
app.config.from_object("lavatar.default_settings")
app.config.from_envvar("LAVATAR_SETTINGS", silent=True)

if not app.debug:
    app.logger.addHandler(logging.StreamHandler())
    app.logger.setLevel(logging.INFO)

redis_store = FlaskRedis(app)
ldap_conn = LDAPConn(app)


class User(ldap_conn.Entry):
    base_dn = app.config["LDAP_USER_BASEDN"]
    object_class = app.config["LDAP_USER_OBJECTCLASS"]
    if isinstance(app.config["LDAP_USER_ATTR_MAIL"], str):
        app.config["LDAP_USER_ATTR_MAIL"] = [app.config["LDAP_USER_ATTR_MAIL"]]
    mail = ldap_conn.Attribute(app.config["LDAP_USER_ATTR_MAIL"][0])
    for i, attr in enumerate(app.config["LDAP_USER_ATTR_MAIL"][1:]):
        exec("mail" + str(i) + "=ldap_conn.Attribute('" + attr + "')")
    photo = ldap_conn.Attribute(app.config["LDAP_USER_ATTR_PHOTO"])


# md5sum thread
Пример #3
0
from flask import Flask
from flask_redis import FlaskRedis

# Set global entities
r = FlaskRedis()


def create_app():
    """Construct the core application."""
    app = Flask(__name__, instance_relative_config=False)
    app.config.from_object('config.Config')

    with app.app_context():
        # Initiate globals
        r.init_app(app, charset="utf-8", decode_responses=True)

        # Set global contexts
        r.set('medium_token', app.config['TOKEN'])
        r.set('medium_client_id', app.config['CLIENT_ID'])
        r.set('medium_client_secret', app.config['CLIENT_SECRET'])
        r.set('medium_publication', app.config['PUBLICATION'])
        r.set('medium_endpoint_me', app.config['ME_ENDPOINT'])

        # Import our modules
        from . import main

        return app
Пример #4
0
"""Simple Flask app connected to Redis"""
import os
from flask import Flask
from flask_redis import FlaskRedis

app = Flask(__name__)
app.config[
    'REDIS_URL'] = f"redis://{os.getenv('REDIS_CONTAINER_NAME', 'redis')}:6379"

redis = FlaskRedis(app)
redis.set_response_callback('GET', int)


def get_count():
    """returns number of visitors as stored in Redis"""
    return redis.get('count')


@app.route('/')
def hello():
    """
    Root path:

    increments the visitor count
    returns welcome message along with visitor count
    """
    redis.incr('count')
    count = get_count()
    s = 's' if count != 1 else ''

    return f"Welcome! {count} whale{s} have docked here."
Пример #5
0
from flask import Flask
import jinja2
from requests import get
from flask_redis import FlaskRedis
app = Flask(__name__)
redis_client = FlaskRedis(app)

SITE_NAME = 'https://games.awdrgyjil1234.repl.co/'


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def proxy(path):
    resp = get(f'{SITE_NAME}{path}')
    if resp.headers.get('content-type'):
        return resp.content
    return resp.text


if __name__ == '__main__':
    app.jinja_env.cache = {}
    app.run(host='0.0.0.0', port=8080)
class RedisClient:
    redis_store = FlaskRedis()
    active = False
    scripts = {}

    def init_app(self, app):
        self.active = app.config.get('REDIS_ENABLED')
        if self.active:
            self.redis_store.init_app(app)

            self.register_scripts()

    def register_scripts(self):

        # delete keys matching a pattern supplied as a parameter. Does so in batches of 5000 to prevent unpack from
        # exceeding lua's stack limit, and also to prevent errors if no keys match the pattern.
        # Inspired by https://gist.github.com/ddre54/0a4751676272e0da8186
        self.scripts[
            'delete-keys-by-pattern'] = self.redis_store.register_script("""
            local keys = redis.call('keys', ARGV[1])
            local deleted = 0
            for i=1, #keys, 5000 do
                deleted = deleted + redis.call('del', unpack(keys, i, math.min(i + 4999, #keys)))
            end
            return deleted
            """)

    def delete_cache_keys_by_pattern(self, pattern):
        r"""
        Deletes all keys matching a given pattern, and returns how many keys were deleted.
        Pattern is defined as in the KEYS command: https://redis.io/commands/keys

        * h?llo matches hello, hallo and hxllo
        * h*llo matches hllo and heeeello
        * h[ae]llo matches hello and hallo, but not hillo
        * h[^e]llo matches hallo, hbllo, ... but not hello
        * h[a-b]llo matches hallo and hbllo

        Use \ to escape special characters if you want to match them verbatim
        """
        if self.active:
            return self.scripts['delete-keys-by-pattern'](args=[pattern])
        return 0

    def exceeded_rate_limit(self,
                            cache_key,
                            limit,
                            interval,
                            raise_exception=False):
        """
        Rate limiting.
        - Uses Redis sorted sets
        - Also uses redis "multi" which is abstracted into pipeline() by FlaskRedis/PyRedis
        - Sends all commands to redis as a group to be executed atomically

        Method:
        (1) Add event, scored by timestamp (zadd). The score determines order in set.
        (2) Use zremrangebyscore to delete all set members with a score between
            - Earliest entry (lowest score == earliest timestamp) - represented as '-inf'
                and
            - Current timestamp minus the interval
            - Leaves only relevant entries in the set (those between now and now - interval)
        (3) Count the set
        (4) If count > limit fail request
        (5) Ensure we expire the set key to preserve space

        Notes:
        - Failed requests count. If over the limit and keep making requests you'll stay over the limit.
        - The actual value in the set is just the timestamp, the same as the score. We don't store any requets details.
        - return value of pipe.execute() is an array containing the outcome of each call.
            - result[2] == outcome of pipe.zcard()
        - If redis is inactive, or we get an exception, allow the request

        :param cache_key:
        :param limit: Number of requests permitted within interval
        :param interval: Interval we measure requests in
        :param raise_exception: Should throw exception
        :return:
        """
        cache_key = prepare_value(cache_key)
        if self.active:
            try:
                pipe = self.redis_store.pipeline()
                when = time()
                pipe.zadd(cache_key, {when: when})
                pipe.zremrangebyscore(cache_key, '-inf', when - interval)
                pipe.zcard(cache_key)
                pipe.expire(cache_key, interval)
                result = pipe.execute()
                return result[2] > limit
            except Exception as e:
                self.__handle_exception(e, raise_exception,
                                        'rate-limit-pipeline', cache_key)
                return False
        else:
            return False

    def set(self,
            key,
            value,
            ex=None,
            px=None,
            nx=False,
            xx=False,
            raise_exception=False):
        key = prepare_value(key)
        value = prepare_value(value)
        if self.active:
            try:
                self.redis_store.set(key, value, ex, px, nx, xx)
            except Exception as e:
                self.__handle_exception(e, raise_exception, 'set', key)

    def incr(self, key, raise_exception=False):
        key = prepare_value(key)
        if self.active:
            try:
                return self.redis_store.incr(key)
            except Exception as e:
                self.__handle_exception(e, raise_exception, 'incr', key)

    def get(self, key, raise_exception=False):
        key = prepare_value(key)
        if self.active:
            try:
                return self.redis_store.get(key)
            except Exception as e:
                self.__handle_exception(e, raise_exception, 'get', key)

        return None

    def decrement_hash_value(self, key, value, raise_exception=False):
        return self.increment_hash_value(key,
                                         value,
                                         raise_exception,
                                         incr_by=-1)

    def increment_hash_value(self,
                             key,
                             value,
                             raise_exception=False,
                             incr_by=1):
        key = prepare_value(key)
        value = prepare_value(value)
        if self.active:
            try:
                return self.redis_store.hincrby(key, value, incr_by)
            except Exception as e:
                self.__handle_exception(e, raise_exception,
                                        'increment_hash_value', key)

    def get_all_from_hash(self, key, raise_exception=False):
        key = prepare_value(key)
        if self.active:
            try:
                return self.redis_store.hgetall(key)
            except Exception as e:
                self.__handle_exception(e, raise_exception,
                                        'get_all_from_hash', key)

    def set_hash_and_expire(self,
                            key,
                            values,
                            expire_in_seconds,
                            raise_exception=False):
        key = prepare_value(key)
        values = {
            prepare_value(k): prepare_value(v)
            for k, v in values.items()
        }
        if self.active:
            try:
                self.redis_store.hmset(key, values)
                return self.redis_store.expire(key, expire_in_seconds)
            except Exception as e:
                self.__handle_exception(e, raise_exception,
                                        'set_hash_and_expire', key)

    def expire(self, key, expire_in_seconds, raise_exception=False):
        key = prepare_value(key)
        if self.active:
            try:
                self.redis_store.expire(key, expire_in_seconds)
            except Exception as e:
                self.__handle_exception(e, raise_exception, 'expire', key)

    def delete(self, *keys, raise_exception=False):
        keys = [prepare_value(k) for k in keys]
        if self.active:
            try:
                self.redis_store.delete(*keys)
            except Exception as e:
                self.__handle_exception(e, raise_exception, 'delete',
                                        ', '.join(keys))

    def __handle_exception(self, e, raise_exception, operation, key_name):
        current_app.logger.exception('Redis error performing {} on {}'.format(
            operation, key_name))
        if raise_exception:
            raise e
Пример #7
0
from flask_redis import FlaskRedis

redis = FlaskRedis()


def configure(app):
    #load all celery tasks
    redis.init_app(app)
Пример #8
0
def static_init_redis():
    """静态方式初始化 redis"""
    for prefix in Config.REDIS_URL_PREFIX:
        redis_cache[prefix] = FlaskRedis(config_prefix=prefix)
Пример #9
0
def get_redis(app):
    return FlaskRedis(app)
Пример #10
0
""" Database and storage related functions and classes """
import datetime
from enum import IntEnum
import functools
import sys
from flask import g
from flask_redis import FlaskRedis
from peewee import IntegerField, DateTimeField, BooleanField, Proxy, Model, Database
from peewee import CharField, ForeignKeyField, TextField, PrimaryKeyField
from werkzeug.local import LocalProxy
from .storage import file_url
from .config import config

rconn = FlaskRedis()

dbp = Proxy()


def get_db():
    if "db" not in g:
        if dbp.is_closed():
            dbp.connect()
        g.db = dbp
    return g.db


db = LocalProxy(get_db)


def db_init_app(app):
    dbconnect = dict(app.config["THROAT_CONFIG"].database)
Пример #11
0
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_redis import FlaskRedis
import pymysql
import os

app = Flask(__name__)
app.config[
    "SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root@localhost:3306/movie"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
app.config["SECRET_KEY"] = 'af2fad8cfe1f4c5fac4aa5edf6fcc8f3'
app.config["REDIS_URL"] = "redis://192.168.4.1:6379/0"
app.config["UP_DIR"] = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                    "static/uploads/")
app.config["FC_DIR"] = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                    "static/uploads/users/")
app.debug = True
db = SQLAlchemy(app)
rd = FlaskRedis(app)

from app.home import home as home_blueprint
from app.admin import admin as admin_blueprint

app.register_blueprint(home_blueprint)
app.register_blueprint(admin_blueprint, url_prefix="/admin")


@app.errorhandler(404)
def page_not_found(error):
    return render_template("home/404.html"), 404
Пример #12
0
from flask_pymongo import PyMongo
from flask_redis import FlaskRedis

mongo = PyMongo()
redis = FlaskRedis(decode_responses=True)
Пример #13
0
#

from flask import Flask
from flask_redis import FlaskRedis

App = Flask(__name__)
App.config.from_object('config')

if 'REDIS_URL' in App.config:
    redis_client = FlaskRedis(App)
    try:
        redis_client.ping()
    except:
        redis_client = None

from app import views