Esempio n. 1
0
from asyncio import gather, new_event_loop, set_event_loop
from dataclasses import dataclass
from datetime import datetime
from json import dumps, loads
from re import findall
from threading import Lock, Thread
from time import sleep

from redis import ConnectionPool, Redis
from requests import session

active_hash = set([])
pool = ConnectionPool(host='localhost', port=6379)
user_list = [
    {
        'account': {
            'name': '',  # 手机号
            'pwd': ''  # 密码
        },
        'address': {
            'name': '天安门',  # 位置名称
            'longitude': 116.402544,  # 经度
            'latitude': 39.91405  # 纬度
        },
        'SCKEY': None,
        'img_path': 'img/cxk.jpeg',  # 路径为 None 时,直接为cxk照片
    },
    # {
    #     'account': {
    #         'name': '',  # 手机号
    #         'pwd': ''  # 密码
Esempio n. 2
0
sentry = Sentry(app)

if not app.debug:
    """
    import etcd

    client = etcd.Client()
    redis_cache_ip = client.read('/nodes/redis-cache').value
    redis_sessions_ip = client.read('/nodes/redis-sessions').value

    """
    redis_cache_ip = '127.0.0.1'
    redis_sessions_ip = '127.0.0.1'
    # REDIS SESSION DATABASE
    redis_session_pool = ConnectionPool(host=redis_sessions_ip,
                                        port=6379,
                                        db=0)
    redis_session = Redis(connection_pool=redis_session_pool)
    # set Flask session_interface to the RedisSessionInterface
    app.session_interface = RedisSessionInterface(redis=redis_session)

    cache = Cache(app,
                  config={
                      'CACHE_TYPE': 'redis',
                      'CACHE_REDIS_HOST': redis_cache_ip,
                      'CACHE_KEY_PREFIX': 'appcache.'
                  })

else:
    cache = Cache(app, config={'CACHE_TYPE': 'simple'})
Esempio n. 3
0
def redis_client(host, port):
    pool = ConnectionPool(host=host, port=port, decode_responses=True)
    r = StrictRedis(connection_pool=pool)
    print("randomkey: ", r.randomkey())
    return r
Esempio n. 4
0
app.config["COMMENT_SPAM_SIMILAR_THRESHOLD"] = float(
    environ.get("COMMENT_SPAM_SIMILAR_THRESHOLD", 0.5))
app.config["COMMENT_SPAM_COUNT_THRESHOLD"] = int(
    environ.get("COMMENT_SPAM_COUNT_THRESHOLD", 5))

app.config["CACHE_REDIS_URL"] = environ.get(
    "REDIS_URL").rstrip().lstrip() if environ.get("REDIS_URL") else None
app.config["CACHE_DEFAULT_TIMEOUT"] = 60
app.config["CACHE_KEY_PREFIX"] = "flask_caching_"

app.config["S3_BUCKET"]=environ.get("S3_BUCKET_NAME","i.ruqqus.com").lstrip().rstrip()

app.config["REDIS_POOL_SIZE"]=int(environ.get("REDIS_POOL_SIZE", 10))

redispool=ConnectionPool(
    max_connections=app.config["REDIS_POOL_SIZE"],
    host=app.config["CACHE_REDIS_URL"][8:]
    ) if app.config["CACHE_TYPE"]=="redis" else None
app.config["CACHE_OPTIONS"]={'connection_pool':redispool} if app.config["CACHE_TYPE"]=="redis" else {}

app.config["READ_ONLY"]=bool(int(environ.get("READ_ONLY", False)))
app.config["BOT_DISABLE"]=bool(int(environ.get("BOT_DISABLE", False)))


Markdown(app)
cache = Cache(app)
Compress(app)

class CorsMatch(str):

    def __eq__(self, other):
        if isinstance(other, str):
Esempio n. 5
0
 def get_redis_connection_pool(self):
     # type: () -> ConnectionPool
     return ConnectionPool(host=self.ip_addr)
Esempio n. 6
0
#  -*- coding:utf-8 -*-

####  Redis 测试

# from redis import StrictRedis
# redis = StrictRedis(host='localhost', port=6379, password='')
# redis.set('name', 'Bod')
# print(redis.get('name'))

# b'Bod'      set()   get()  方法 正常

from redis import StrictRedis, ConnectionPool
pool = ConnectionPool(host='localhost', port=6379, db=0, password='')
redis = StrictRedis(connection_pool=pool)
redis.set('name', 'Bod')
print(redis.get('name'))

# b'Bod'
# 观察源码可以发现,StrictRedis内其实就是用host和port等参数又构造了一个ConnectionPool,
#  所以直接将ConnectionPool当作参数传给StrictRedis也一样

# ConnectionPool还支持通过URL来构建。URL的格式支持有如下3种

# redis://[:password]@host:port/db        Redis TCP连接
# rediss://[:password]@host:port/db       Redis TCP+SSL连接
# unix://[:password]@/path/to/socket.sock?db=db         Redis UNIX socket连接

from redis import StrictRedis, ConnectionPool
url = 'redis://@localhost:6379/0'
pool = ConnectionPool.from_url(url)
redis = StrictRedis(connection_pool=pool)
Esempio n. 7
0
# coding=utf-8
import os

from redis import ConnectionPool, StrictRedis

redis_pool = ConnectionPool(
    host=os.environ.get("REDIS_HOST", "localhost"),
    port=os.environ.get("REDIS_PORT", 6379),
    db=os.environ.get("REDIS_DB", 0),
)


def create_redis() -> StrictRedis:
    return StrictRedis(
        connection_pool=redis_pool
    )
Esempio n. 8
0
from distutils.version import StrictVersion
from redis.exceptions import NoScriptError
from redis import Redis, ConnectionPool

# Adapted from http://redis.io/commands/incr#pattern-rate-limiter-2
INCREMENT_SCRIPT = b"""
    local current
    current = tonumber(redis.call("incr", KEYS[1]))
    if current == 1 then
        redis.call("expire", KEYS[1], ARGV[1])
    end
    return current
"""
INCREMENT_SCRIPT_HASH = sha1(INCREMENT_SCRIPT).hexdigest()

REDIS_POOL = ConnectionPool(host='127.0.0.1', port=6379, db=0)


def fibonacci(n):
    a, b = 0, 1
    for i in range(0, n):
        a, b = b, a + b
    return a


class RedisVersionNotSupported(Exception):
    """
    Rate Limit depends on Redis’ commands EVALSHA and EVAL which are
    only available since the version 2.6.0 of the database.
    """
    pass
Esempio n. 9
0
    def exsits(self, value, name):
        if value is None:
            return False
        if value.__len__() == 0:
            return False
        ret = True
        hashs = hashlib.md5(value.encode('utf-8')).hexdigest()
        for f in self.hashFunc:
            loc = f.hash(hashs)
            ret = ret & self.r.getbit(name, loc)
        return ret

    def insert(self, value, name):
        if 'test' in sys.argv:
            return
        hashs = hashlib.md5(value.encode('utf-8')).hexdigest()
        for f in self.hashFunc:
            loc = f.hash(hashs)
            self.r.setbit(name, loc, 1)


if __name__ == '__main__':
    from redis import StrictRedis, ConnectionPool

    bf = BloomFilter(StrictRedis(connection_pool=ConnectionPool()))
    # for i in xrange(0,1000000,100):
    #     bf.insert(str(i),'test')
    for i in xrange(0, 10000, 100):
        if not bf.exsits(str(i), 'test'):
            print i
Esempio n. 10
0
# _*_ coding:utf-8 _*_
# @File  : redis_z.py
# @Time  : 2020-08-31 8:28
# @Author: zizle
from redis import Redis, ConnectionPool
from configs import DB_CONFIGS

params = DB_CONFIGS["redis"]

conn_pool = ConnectionPool(decode_responses=True,
                           **params)  # 利用文件导入的"单例"模式设置连接池


class RedisZ(object):
    def __init__(self):
        self.redis_conn = Redis(connection_pool=conn_pool)

    def __enter__(self):
        return self.redis_conn

    def __exit__(self, exc, value, traceback):
        self.redis_conn.close()
Esempio n. 11
0
# -*- coding: utf-8 -*-
# @Time    : 2018/10/24 0024 20:54
# @Author  : jiaojianglong

from tornado.web import RequestHandler
from tornado import gen
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
import time
import json

from rq import Queue
from redis import Redis, ConnectionPool
redis_conn = ConnectionPool(
    host="127.0.0.1",
    port=6379,
    db=1,
)
redis_conn = Redis(connection_pool=redis_conn)
q = Queue(connection=redis_conn)


class Basehandler(RequestHandler):
    pass


class AsyncHandler(Basehandler):
    @gen.coroutine
    def get(self):
        try:
            ret = yield self.sleep()
Esempio n. 12
0
from redis import ConnectionPool, StrictRedis
from ws4redis import settings
from ws4redis.redis_store import RedisStore

redis_connection_pool = ConnectionPool(**settings.WS4REDIS_CONNECTION)


class RedisPublisher(RedisStore):
    def __init__(self, **kwargs):
        """
        Initialize the channels for publishing messages through the message queue.
        """
        connection = StrictRedis(connection_pool=redis_connection_pool)
        super(RedisPublisher, self).__init__(connection)
        for key in self._get_message_channels(**kwargs):
            self._publishers.add(key)

    def fetch_message(self, request, facility, audience='any'):
        """
        Fetch the first message available for the given ``facility`` and ``audience``, if it has
        been persisted in the Redis datastore.
        The current HTTP ``request`` is used to determine to whom the message belongs.
        A unique string is used to identify the bucket's ``facility``.
        Determines the ``audience`` to check for the message. Must be one of ``broadcast``,
        ``group``, ``user``, ``session`` or ``any``. The default is ``any``, which means to check
        for all possible audiences.
        """
        prefix = self.get_prefix()
        channels = []
        if audience in (
                'session',
Esempio n. 13
0
from .base import *
import environ
from redis import ConnectionPool

env = environ.Env()
env.read_env(os.path.join(BASE_DIR, 'env/.env.development'))

DEBUG = True

REDIS_CONNECTION_POOL = ConnectionPool(host=env('REDIS_HOST'),
                                       port=env('REDIS_PORT'),
                                       db=0)

# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': env('MYSQL_DB_NAME'),
        'USER': env('MYSQL_DB_USER'),
        'PASSWORD': env('MYSQL_DB_PW'),
        'HOST': env('MYSQL_DB_HOST'),
        'PORT': env('MYSQL_DB_PORT'),
    }
}
import pymysql
import threading
import re
import time
from queue import Queue
from DBUtils.PooledDB import PooledDB
import pymysql

from redis import ConnectionPool, Redis
pool = ConnectionPool(host='s40', port=6379, db=0, decode_responses=True)
rdb = Redis(connection_pool=pool)
class ThreadInsert(object):
    "多线程并发MySQL插入数据"
    def __init__(self):
        start_time = time.time()
        self.pool = self.mysql_connection()
        self.data = self.getData()
        self.mysql_delete()
        self.task()
        print("========= 数据插入,共耗时:{}'s =========".format(round(time.time() - start_time, 3)))

    def mysql_connection(self):
        maxconnections = 15  # 最大连接数
        pool = PooledDB(
            pymysql,
            maxconnections,
            host='localhost',
            user='******',
            port=3306,
            passwd='123456',
            db='article_spider',
Esempio n. 15
0
import os
import stripe

from functools import wraps

from flask import g, request, session
from redis import ConnectionPool, StrictRedis

from sqlalchemy.orm.exc import NoResultFound

from gpbilling.model import sm, Account

redis_pool = ConnectionPool(
    host=os.environ.get('REDIS_PORT_6379_TCP_ADDR',
                        os.environ.get('REDIS_HOST', '127.0.0.1')),
    port=int(
        os.environ.get('REDIS_PORT_6379_TCP_PORT',
                       os.environ.get('REDIS_PORT', 6379))),
    db=int(os.environ.get('REDIS_DB', 0)),
    decode_responses=True)


def init_stripe():
    plans = {
        "email": {
            "amount": 1000,
            "interval": "month",
            "product": {
                "name": "Email service"
            },
            "nickname": "Email service",
            "currency": "usd",
Esempio n. 16
0
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('POSTGRES_DB', 'sciwms'),
        'USER': os.environ.get('POSTGRES_USER', 'sciwms'),
        'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'sciwms'),
        'HOST': os.environ.get('POSTGRES_HOST', 'db'),
        'PORT': os.environ.get('POSTGRES_PORT', 5432),
    },
}

# Redis
redis_host = os.environ.get('REDIS_HOST', 'redis')
redis_port = int(os.environ.get('REDIS_PORT', 6379))
redis_db = int(os.environ.get('REDIS_DB', 0))

pool = ConnectionPool(host=redis_host, port=redis_port, db=redis_db)

# Huey
HUEY = {
    'name': 'sciwms',
    'result_store': True,  # Store return values of tasks.
    'events': True,  # Consumer emits events allowing real-time monitoring.
    'store_none': True,  # If a task returns None, do not save to results.
    'always_eager': False,  # If DEBUG=True, run synchronously.
    'store_errors': True,  # Store error info if task throws exception.
    'blocking': False,  # Poll the queue rather than do blocking pop.
    'backend_class': 'huey.RedisHuey',  # Use path to redis huey by default,
    'connection': {
        'connection_pool': pool
    },
    'consumer': {
Esempio n. 17
0
import os
import logging
from flask import Flask
from redis import ConnectionPool

REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
REDIS_PORT = int(os.environ.get('REDIS_PORT', '6379'))
redis_pool = ConnectionPool(host=REDIS_HOST, port=REDIS_PORT)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


app = Flask(__name__)

Esempio n. 18
0
"""
Redis related module
"""

from redis import ConnectionPool, Redis

from sni.conf import CONFIGURATION as conf

connection_pool = ConnectionPool(
    db=conf.redis.database,
    host=conf.redis.host,
    port=conf.redis.port,
)


def new_redis_connection() -> Redis:
    """
    Returns a new redis connection handler
    """
    return Redis(connection_pool=connection_pool)
Esempio n. 19
0
 def create_redis(self):
     global REDIS, redisPool
     config = self.configs[self.REDIS]
     hosts, port, user, password = self.get_params(config)
     self.redisPool = ConnectionPool(host=hosts, port=port)
Esempio n. 20
0
from tornado_json.application import Application
from tornado_json.routes import get_routes
from tornadouvloop import TornadoUvloop

import admin
import box
import game
import prizes
import referral
import tips
import user
from face_auth import FacebookGraphLoginHandler
from utils import SessionBaseHandler

pool = ConnectionPool(max_connections=2,
                      host='session.sqippd.0001.use1.cache.amazonaws.com',
                      port=6379,
                      db=0)

redis = Redis(connection_pool=pool)


class HomeHandler(SessionBaseHandler):
    def get(self):
        self.finish({
            'success': True,
            'user_id': self.session.get('id'),
            'name': self.session.get('name'),
            'token': self.session.getCookieOrToken()
        })

"""
Created on 2017/7/17
@author: MG
"""
from threading import Thread
from redis import StrictRedis, ConnectionPool
import time
from datetime import datetime
import logging
from src.fh_tools.fh_utils import bytes_2_str
import json
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s: %(levelname)s [%(name)s:%(funcName)s] %(message)s')
logger = logging.getLogger()
conn_pool = ConnectionPool(host='192.168.159.131', port=6379, db=1)


class Productor(Thread):
    def run(self):
        r = StrictRedis(connection_pool=conn_pool)

        for n in range(10):
            if n == 9:
                r.publish('pubsub_msg', 'over')
            else:
                r.publish('pubsub_msg', {'pub_time': datetime.now()})
            time.sleep(1)


class Consumer(Thread):
Esempio n. 22
0
def main(pargs):
    global redis
    if pargs.production:
        serverConf = configobj.ConfigObj('production.ini')
    else:
        serverConf = configobj.ConfigObj('develop.ini')

    routes = [
        (r"/auth/login", FacebookGraphLoginHandler),
        (r"/", HomeHandler),
        (r"/referral/([^/]+)", ReferralHandler),
        (r"/r/([^/]+)", ReferralHandler),
        (r'/(favicon.ico)', StaticFileHandler, {
            "path": "./static/"
        }),
    ]

    for api in [game, tips, user, referral, prizes]:
        routes.extend(get_routes(api))

    if pargs.admin:
        routes.extend(get_routes(admin))
        routes.extend(get_routes(box))
        from encontact import UserDataHandler
        routes.extend([
            (r"/admin/encontact?/", UserDataHandler),
        ])

    pprint(routes, indent=4)

    mailer = Mail(aws_access_key_id=serverConf['ses']['acceskeyid'],
                  aws_secret_access_key=serverConf['ses']['secretacceskeyid'],
                  region=serverConf['ses']['region'],
                  sender=serverConf['ses']['sender'],
                  template=serverConf['ses']['templates'])

    if pargs.production:
        dyn = DynamoDBConnection.connect(
            region=serverConf['dynamo']['region'],
            access_key=serverConf['dynamo']['acceskeyid'],
            secret_key=serverConf['dynamo']['secretacceskeyid'])
        session_settings = dict(
            driver="redis",
            force_persistence=True,
            cache_driver=True,
            driver_settings=dict(
                host=serverConf['redis']['host'],
                port=int(serverConf['redis']['port']),
                db=int(serverConf['redis']['db']),
                max_connections=1024,
            ),
        )
        pool = ConnectionPool(max_connections=2,
                              host=serverConf['redis']['host'],
                              port=int(serverConf['redis']['port']),
                              db=int(serverConf['redis']['db']) + 1)

        pool2 = ConnectionPool(max_connections=2,
                               host=serverConf['redis']['host'],
                               port=int(serverConf['redis']['port']),
                               db=int(serverConf['redis']['db']))

    else:
        dyn = DynamoDBConnection.connect(region='sp-east',
                                         host='127.0.0.1',
                                         port=8000,
                                         is_secure=False,
                                         access_key='asdas',
                                         secret_key='123ads')
        session_settings = dict(
            driver="redis",
            force_persistence=True,
            cache_driver=True,
            driver_settings=dict(
                host='localhost',
                port=6379,
                db=0,
                max_connections=1024,
            ),
        )
        pool = ConnectionPool(max_connections=2,
                              host='localhost',
                              port=6379,
                              db=1)

        pool2 = ConnectionPool(max_connections=2,
                               host='localhost',
                               port=6379,
                               db=0)

    redis = Redis(connection_pool=pool)
    redis2 = Redis(connection_pool=pool2)
    engine = Engine(dynamo=dyn)
    log = logging.getLogger(__name__)
    a = logging.basicConfig(
        level=logging.INFO,
        format=
        '[ %(asctime)s ][ %(levelname)s ][ %(filename)20s:%(lineno)4s - %(funcName)20s() ] %(message)s',
        datefmt='%m-%d %H:%M',
        filename='log/nextgame.log',
        filemode='a')

    log.addHandler(a)
    settings = {
        "debug": False,
        'xsrf_cookies': False,
        'serverConfig': serverConf,
        'instance': serverConf.get('instance'),
        'engine': engine,
        'facebook_api_key': serverConf['facebook']['key'],
        'facebook_secret': serverConf['facebook']['secret'],
        'session': session_settings,
        'template_path': serverConf['ses']['templates'],
        "login_url": "/auth/login/",
        "cookie_secret": 'sopadeletrinhas123',
        "mailer": mailer,
        "production": False
    }

    if (pargs.debug):
        # log.setLevel(logging.DEBUG)
        log.debug('DEBUGGING LOG')
        settings['debug'] = True

    app = Application(routes=routes,
                      settings=settings,
                      db_conn={
                          'ping': redis,
                          'session': redis2
                      })

    if pargs.provision:
        game.models.create_schemas(engine)
        tips.models.create_schemas(engine)
        referral.models.create_schemas(engine)

    if (pargs.production):
        print('Server Production Starting')
        server = tornado.httpserver.HTTPServer(app)
        server.bind(serverConf['tornado']['port'])
        server.start(int(serverConf['tornado']['instances']))
        tornado.ioloop.IOLoop.configure(TornadoUvloop)

    else:
        print('Server Develop Starting')
        app.listen(serverConf['tornado']['port'])
        tornado.ioloop.IOLoop.configure(TornadoUvloop)

    tornado.ioloop.IOLoop.current().start()
# @Time : 2020/3/11 下午12:41
# @Author: [email protected]
# @File : video_stream_validate.py
# 视频地址验证
import argparse
import requests
import threading
import logging
from datetime import datetime
import time
import redis
import sys
from redis import ConnectionPool

POOL = ConnectionPool(host='127.0.0.1',
                      port=6379,
                      max_connections=100,
                      decode_responses=True)
conn = redis.Redis(connection_pool=POOL)
# 采用redis hashmap存储视频状态
# key= 'camera_'+cameraNum
# fields:
#   onlineStatus:0不在线 1在线 2异常;我们检查状态为1的,如果可用更新为11,不可用更新为10
#   updateTime: 更新时间
#   reason: 不可用的原因
#   mode: PRO生产 TEST测试

key_prefix = 'camera_'
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
import os

from huey import PriorityRedisExpireHuey
from redis import ConnectionPool

pool = ConnectionPool(host=os.environ.get('REDIS_HOST', 'redis'),
                      port=int(os.environ.get('REDIS_PORT', 6379)),
                      max_connections=20)
HUEY = PriorityRedisExpireHuey(
    name='huey_monitor_tests',  # Just the name for this task queue.
    connection_pool=pool,  # Use a connection pool to redis.
    results=True,  # Store return values of tasks.
    store_none=False,  # If a task returns None, do not save to results.
    utc=True,  # Use UTC for all times internally.
    expire_time=24 * 60 *
    60,  # cleaned-up unread task results from Redis after 24h
)
Esempio n. 25
0
TESTING_SCHEDULE = {"hour": "*/1", "minute": "0"}

# Backup

BACKUP_FOLDER = "backups"

BACKUP_SCHEDULE = {"hour": "23", "minute": "0"}

DEADLINES_SCHEDULE = {"hour": "7", "minute": "0"}

BACKUP_PATH = os.path.join(BASE_DIR, BACKUP_FOLDER)

FILE_DAYS_OLD_TO_REMOVE = os.environ.get("FILE_DAYS_OLD_TO_REMOVE", 7)

pool = ConnectionPool(host=os.environ.get("REDIS_HOST", "localhost"),
                      port=6379,
                      max_connections=20)
HUEY = RedisHuey("handymantasks", connection_pool=pool)

try:
    from .local_settings import *  # NOQA ;
except ImportError:
    print(
        "\n\nWARNING: No local_settings.py found! Please look at the README.md file!\n\n"
    )

try:
    os.makedirs(BACKUP_PATH)
except OSError:
    print("Backup Directory: {0} already exists!".format(BACKUP_PATH))
Esempio n. 26
0
EMAIL_PORT = 587

if DEBUG:
    ALLOWED_HOSTS = []
    # https://huey.readthedocs.io/en/latest/django.html
    HUEY = {
        'huey_class': 'huey.SqliteHuey',
        'name': DATABASES['default']['NAME'],
        'immediate': False,
        # Options to pass into the consumer when running ``manage.py run_huey``
        'consumer': {
            'workers': 1,
            'worker_type': 'thread',
        },
    }
else:
    ALLOWED_HOSTS = ['cybereyeproject.herokuapp.com']
    # Security
    SECURE_SSL_REDIRECT = True
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
    # add credentials
    pool = ConnectionPool()
    HUEY = RedisHuey('cybereye', connection_pool=pool)

# Keep this at the very bottom of the file - This sets a lot of configs for django and heroku
django_heroku.settings(locals())
"""
Write stuff to make file commit
"""
Esempio n. 27
0
 def setUp(self):
     pool = ConnectionPool(host=redis_endpoint.address.host,
                           port=redis_endpoint.address.port)
     self.backend_factory = RedisRateLimitBackendContextFactory(pool)
     super(RedisRateLimitBackendTests, self).setUp()
Esempio n. 28
0
import functools
from redis import Redis, ConnectionPool
from config import CONNECTION, LIMITING

REDIS_CONNECTION_CONFIG = ConnectionPool(host=CONNECTION["host_address"],
                                         port=CONNECTION["port"],
                                         db=CONNECTION["db"])


def keyFormat(ip_addr: str) -> str:
    '''
    Formats a redis key with the prefix from config

    # Arguments:
    - ip_addr <str> :: IP address of the request (E.g. "104.25.94.32")
    '''
    return LIMITING["key_prefix"] + "{0}".format(ip_addr)


class RateLimiter:
    def __init__(self,
                 client_addr: str,
                 max_reqs: int = LIMITING["max_reqs"],
                 expiry: int = LIMITING["expiry"]):
        '''
        Creates a rate limiter handler for a given client address (request IP) with
        given arguments

        # Arguments:
        - client_addr <str> :: IP address of the request (E.g "104.25.94.32")
        - max_regs <int> :: Maximum amount of requests for duration
Esempio n. 29
0
    def __init__(self,
                 urls,
                 username=None,
                 password=None,
                 redis_mode=RedisMode.STANDALONE,
                 timeout=5,
                 master_name=None,
                 db=0,
                 decode_responses=True):
        """

        :param urls: redis 地址 ('hostname', 6379) 或 [('hostname', 6379),('hostname', 6378)] 或 [{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"}]
        :param password: auth
        :param redis_mode: @see RedisMode
        :param timeout:
        :param master_name:
        :param db:
        :param decode_responses:
        """
        self.urls = urls
        self.username = username
        self.password = password
        self.redis_mode = redis_mode
        self.timeout = timeout
        self.master_name = master_name
        self.db = db

        self.__conn = None
        self.__pool = None
        self.__cluster = None
        if self.redis_mode == RedisMode.SENTINEL or self.redis_mode == 1:
            if isinstance(urls, str):
                urls = [url.split(":") for url in urls.split(";")]
            if not isinstance(urls, List) and not isinstance(urls, Tuple):
                raise TypeError(
                    "url : [('hostname', 6379),('hostname', 6378)]")
            sentinel = Sentinel(urls,
                                socket_timeout=self.timeout,
                                db=self.db,
                                username=username,
                                password=self.password,
                                decode_responses=decode_responses)
            self.__pool = SentinelConnectionPool(master_name,
                                                 sentinel,
                                                 password=self.password)
        elif self.redis_mode == RedisMode.CLUSTER or self.redis_mode == 2:
            if isinstance(urls, str):

                def addr(url):
                    _host, _port = url.split(":")
                    return {"host": _host, "port": _port}

                urls = [addr(url) for url in urls.split(";")]

            if not isinstance(urls, List) and not isinstance(urls, Tuple):
                raise TypeError(
                    'url : [{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"}]'
                )
            self.__cluster = RedisCluster(startup_nodes=urls,
                                          decode_responses=decode_responses,
                                          socket_timeout=self.timeout,
                                          db=self.db,
                                          username=username,
                                          password=self.password)
        elif self.redis_mode == RedisMode.STANDALONE or self.redis_mode == 0:
            if isinstance(urls, str):
                urls = urls.split(":")
            if not isinstance(urls, List) and not isinstance(urls, Tuple):
                raise TypeError("url : ('hostname', 6379)")
            hostname, port = urls
            self.__pool = ConnectionPool(host=hostname,
                                         port=port,
                                         socket_timeout=self.timeout,
                                         password=self.password,
                                         db=self.db,
                                         username=username,
                                         decode_responses=decode_responses)
        else:
            raise TypeError('redis mode err')
Esempio n. 30
0
from redis import StrictRedis, ConnectionPool
import hashlib
import requests

pool = ConnectionPool(host='localhost', port=6379, decode_responses=True)
redis = StrictRedis(connection_pool=pool)

class CacheTool:

    @staticmethod
    def hsetnx(key,field,val):
        return redis.hsetnx(key,field,val)

    @staticmethod
    def set(name,value):
        return redis.set(name,value)

    def get(name):
        return redis.get(name)

    @staticmethod
    def sadd(value):
        return redis.sadd('url',value)

    @staticmethod
    def smembers():
        return redis.smembers('url')
    
    @staticmethod
    def rpush(key,val):
        return redis.rpush(key,val)