11import redis
22from proxypool .exceptions import PoolEmptyException
33from proxypool .schemas .proxy import Proxy
4- from proxypool .setting import REDIS_HOST , REDIS_PORT , REDIS_PASSWORD , REDIS_DB , REDIS_KEY , PROXY_SCORE_MAX , PROXY_SCORE_MIN , \
4+ from proxypool .setting import REDIS_CONNECTION_STRING , REDIS_HOST , REDIS_PORT , REDIS_PASSWORD , REDIS_DB , REDIS_KEY , PROXY_SCORE_MAX , PROXY_SCORE_MIN , \
55 PROXY_SCORE_INIT
66from random import choice
77from typing import List
@@ -18,14 +18,21 @@ class RedisClient(object):
1818 redis connection client of proxypool
1919 """
2020
21- def __init__ (self , host = REDIS_HOST , port = REDIS_PORT , password = REDIS_PASSWORD , db = REDIS_DB , ** kwargs ):
21+ def __init__ (self , host = REDIS_HOST , port = REDIS_PORT , password = REDIS_PASSWORD , db = REDIS_DB ,
22+ connection_string = REDIS_CONNECTION_STRING , ** kwargs ):
2223 """
2324 init redis client
2425 :param host: redis host
2526 :param port: redis port
2627 :param password: redis password
28+ :param connection_string: redis connection_string
2729 """
28- self .db = redis .StrictRedis (host = host , port = port , password = password , db = db , decode_responses = True , ** kwargs )
30+ # if set connection_string, just use it
31+ if connection_string :
32+ self .db = redis .StrictRedis .from_url (connection_string )
33+ else :
34+ self .db = redis .StrictRedis (
35+ host = host , port = port , password = password , db = db , decode_responses = True , ** kwargs )
2936
3037 def add (self , proxy : Proxy , score = PROXY_SCORE_INIT ) -> int :
3138 """
@@ -51,11 +58,13 @@ def random(self) -> Proxy:
5158 :return: proxy, like 8.8.8.8:8
5259 """
5360 # try to get proxy with max score
54- proxies = self .db .zrangebyscore (REDIS_KEY , PROXY_SCORE_MAX , PROXY_SCORE_MAX )
61+ proxies = self .db .zrangebyscore (
62+ REDIS_KEY , PROXY_SCORE_MAX , PROXY_SCORE_MAX )
5563 if len (proxies ):
5664 return convert_proxy_or_proxies (choice (proxies ))
5765 # else get proxy by rank
58- proxies = self .db .zrevrange (REDIS_KEY , PROXY_SCORE_MIN , PROXY_SCORE_MAX )
66+ proxies = self .db .zrevrange (
67+ REDIS_KEY , PROXY_SCORE_MIN , PROXY_SCORE_MAX )
5968 if len (proxies ):
6069 return convert_proxy_or_proxies (choice (proxies ))
6170 # else raise error
@@ -125,4 +134,3 @@ def batch(self, cursor, count) -> List[Proxy]:
125134 conn = RedisClient ()
126135 result = conn .random ()
127136 print (result )
128-
0 commit comments