DbClient DB工厂类 提供get/put/update/pop/delete/exists/getAll/clean/getCount/changeTable方法 抽象方法定义: get(): 随机返回一个proxy; put(proxy): 存入一个proxy; pop(): 顺序返回并删除一个proxy; update(proxy): 更新指定proxy信息; delete(proxy): 删除指定proxy; exists(proxy): 判断指定proxy是否存
| 24 | |
| 25 | |
| 26 | class DbClient(withMetaclass(Singleton)): |
| 27 | """ |
| 28 | DbClient DB工厂类 提供get/put/update/pop/delete/exists/getAll/clean/getCount/changeTable方法 |
| 29 | |
| 30 | |
| 31 | 抽象方法定义: |
| 32 | get(): 随机返回一个proxy; |
| 33 | put(proxy): 存入一个proxy; |
| 34 | pop(): 顺序返回并删除一个proxy; |
| 35 | update(proxy): 更新指定proxy信息; |
| 36 | delete(proxy): 删除指定proxy; |
| 37 | exists(proxy): 判断指定proxy是否存在; |
| 38 | getAll(): 返回所有代理; |
| 39 | clean(): 清除所有proxy信息; |
| 40 | getCount(): 返回proxy统计信息; |
| 41 | changeTable(name): 切换操作对象 |
| 42 | |
| 43 | |
| 44 | 所有方法需要相应类去具体实现: |
| 45 | ssdb: ssdbClient.py |
| 46 | redis: redisClient.py |
| 47 | mongodb: mongodbClient.py |
| 48 | |
| 49 | """ |
| 50 | |
| 51 | def __init__(self, db_conn): |
| 52 | """ |
| 53 | init |
| 54 | :return: |
| 55 | """ |
| 56 | self.parseDbConn(db_conn) |
| 57 | self.__initDbClient() |
| 58 | |
| 59 | @classmethod |
| 60 | def parseDbConn(cls, db_conn): |
| 61 | db_conf = urlparse(db_conn) |
| 62 | cls.db_type = db_conf.scheme.upper().strip() |
| 63 | cls.db_host = db_conf.hostname |
| 64 | cls.db_port = db_conf.port |
| 65 | cls.db_user = db_conf.username |
| 66 | cls.db_pwd = db_conf.password |
| 67 | cls.db_name = db_conf.path[1:] |
| 68 | return cls |
| 69 | |
| 70 | def __initDbClient(self): |
| 71 | """ |
| 72 | init DB Client |
| 73 | :return: |
| 74 | """ |
| 75 | __type = None |
| 76 | if "SSDB" == self.db_type: |
| 77 | __type = "ssdbClient" |
| 78 | elif "REDIS" == self.db_type: |
| 79 | __type = "redisClient" |
| 80 | else: |
| 81 | pass |
| 82 | assert __type, 'type error, Not support DB type: {}'.format(self.db_type) |
| 83 | self.client = getattr(__import__(__type), "%sClient" % self.db_type.title())(host=self.db_host, |