forked from jhao104/proxy_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbClient.py
100 lines (79 loc) · 2.67 KB
/
DbClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
# !/usr/bin/env python
"""
-------------------------------------------------
File Name: DbClient.py
Description : DB工厂类
Author : JHao
date: 2016/12/2
-------------------------------------------------
Change Activity:
2016/12/2:
-------------------------------------------------
"""
__author__ = 'JHao'
import os
import sys
from Util.GetConfig import GetConfig
from Util.utilClass import Singleton
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
class DbClient(object):
"""
DbClient DB工厂类 提供get/put/pop/delete/getAll/changeTable方法
目前存放代理的table/collection/hash有两种:
raw_proxy: 存放原始的代理;
useful_proxy_queue: 存放检验后的代理;
抽象方法定义:
get: 随机返回一个代理;
put: 放回一个代理;
delete: 删除指定代理;
getAll: 返回所有代理;
changeTable: 切换 table or collection or hash
所有方法需要相应类去具体实现:
SSDB:SsdbClient.py
REDIS:RedisClient.py 只是对redis set的操作,不一定好复用
"""
__metaclass__ = Singleton
def __init__(self):
"""
init
:return:
"""
self.config = GetConfig()
self.__initDbClient()
def __initDbClient(self):
"""
init DB Client
:return:
"""
__type = None
if "SSDB" == self.config.db_type:
__type = "SsdbClient"
elif "REDIS" == self.config.db_type:
__type = "RedisClient"
else:
pass
assert __type, 'type error, Not support DB type: {}'.format(self.config.db_type)
self.client = getattr(__import__(__type), __type)(name=self.config.db_name,
host=self.config.db_host,
port=self.config.db_port)
def get(self, **kwargs):
return self.client.get(**kwargs)
def put(self, value, **kwargs):
return self.client.put(value, **kwargs)
def pop(self, **kwargs):
return self.client.pop(**kwargs)
def delete(self, value, **kwargs):
return self.client.delete(value, **kwargs)
def getAll(self):
return self.client.getAll()
def changeTable(self, name):
self.client.changeTable(name)
def get_status(self):
return self.client.get_status()
if __name__ == "__main__":
account = DbClient()
print(account.get())
account.changeTable('use')
account.put('ac')
print(account.get())