Ver Fonte

完善Redis和Mongodb验证功能

添加Config.ini的用户和密码

为username参数做兼容处理
Jacob há 7 anos atrás
pai
commit
8ac170e981
6 ficheiros alterados com 23 adições e 7 exclusões
  1. 2 1
      Config.ini
  2. 1 0
      DB/DbClient.py
  3. 2 2
      DB/MongodbClient.py
  4. 6 2
      DB/RedisClient.py
  5. 5 2
      DB/SsdbClient.py
  6. 7 0
      Util/GetConfig.py

+ 2 - 1
Config.ini

@@ -6,7 +6,8 @@ host = 127.0.0.1
 port = 6379
 ;port = 8888
 name = proxy
-#password = yourpassword
+;username = your_username (Only Mongodb)
+;password = your_password
 
 [ProxyGetter]
 ;register the proxy getter function

+ 1 - 0
DB/DbClient.py

@@ -76,6 +76,7 @@ class DbClient(object):
         self.client = getattr(__import__(__type), __type)(name=self.config.db_name,
                                                           host=self.config.db_host,
                                                           port=self.config.db_port,
+                                                          username=self.config.db_username,
                                                           password=self.config.db_password)
 
     def get(self, key, **kwargs):

+ 2 - 2
DB/MongodbClient.py

@@ -17,9 +17,9 @@ from pymongo import MongoClient
 
 
 class MongodbClient(object):
-    def __init__(self, name, host, port):
+    def __init__(self, name, host, port, **kwargs):
         self.name = name
-        self.client = MongoClient(host, port)
+        self.client = MongoClient(host, port, **kwargs)
         self.db = self.client.proxy
 
     def changeTable(self, name):

+ 6 - 2
DB/RedisClient.py

@@ -22,7 +22,11 @@ class RedisClient(object):
     Reids client
     """
 
-    def __init__(self, name, host, port):
+    # 为了保持DbClient的标准
+    # 在RedisClient里面接受username参数, 但不进行使用.
+    # 因为不能将username通过kwargs传进redis.Redis里面, 会报错:
+    # TypeError: __init__() got an unexpected keyword argument 'username'
+    def __init__(self, name, host, port, username, **kwargs):
         """
         init
         :param name:
@@ -31,7 +35,7 @@ class RedisClient(object):
         :return:
         """
         self.name = name
-        self.__conn = redis.Redis(host=host, port=port, db=0)
+        self.__conn = redis.Redis(host=host, port=port, db=0, **kwargs)
 
     def get(self):
         """

+ 5 - 2
DB/SsdbClient.py

@@ -31,8 +31,11 @@ class SsdbClient(object):
         验证后的代理存放在name为useful_proxy的hash中,key为代理的ip:port,value为一个计数,初始为1,每校验失败一次减1;
 
     """
-
-    def __init__(self, name, **kwargs):
+    # 为了保持DbClient的标准
+    # 在SsdbClient里面接受username参数, 但不进行使用.
+    # 因为不能将username通过kwargs传进redis.Redis里面, 会报错:
+    # TypeError: __init__() got an unexpected keyword argument 'username'
+    def __init__(self, name, username, **kwargs):
         """
         init
         :param name: hash name

+ 7 - 0
Util/GetConfig.py

@@ -53,6 +53,13 @@ class GetConfig(object):
             password = None
         return password
 
+    @LazyProperty
+    def db_username(self):
+        try:
+            username = self.config_file.get('DB', 'username')
+        except Exception:
+            username = None
+        return username
 
     @LazyProperty
     def proxy_getter_functions(self):