修改了配置文件,加入redis_index

修改redis连接代码
This commit is contained in:
陈冠林
2018-12-04 16:22:08 +08:00
parent df7f755035
commit b6021cd634
2 changed files with 18 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
[server]
server_pwd=111111
[redis]
host=192.168.10.180
port=26379
host=192.168.11.243
port=6800
index=5

View File

@@ -29,12 +29,13 @@ response_dic={'miss':'miss parameters.',
# "outer_dip","outer_smac","outer_dmac","inner_smac","inner_dmac",\
# "is_valid","op_time"]
class Redis:
def __init__(self,host,port):
def __init__(self,host,port,index):
self.host=host
self.port=port
self.index=index
def connect(self):
try:
pool = redis.ConnectionPool(host=self.host, port=self.port, db=0,decode_responses=True)
pool = redis.ConnectionPool(host=self.host, port=self.port, db=self.index,decode_responses=True)
r = redis.StrictRedis(connection_pool=pool)
except redis.RedisError as e:# Exception,e:print(str(e))
print("Error:"+str(e))
@@ -58,7 +59,7 @@ class Redis:
# Create your views here.
def command(request):
# if username == "" and password = "" :
server_pwd,host,port=readconfig()
server_pwd,host,port,index=readconfig()
if request.method=='GET':
cmd_obj=request.GET.get('cmd_obj',default='server')
if cmd_obj=='server':
@@ -78,7 +79,7 @@ def command(request):
myresponse=HttpResponse(json.dumps(my_dict),content_type='application/json',status=400)
return myresponse
else:
rtn_code,outs=IpExist(host,port,candidate_ip)
rtn_code,outs=IpExist(host,port,candidate_ip,index)
my_dict=dict()
if(rtn_code==0):
my_dict['response']=outs
@@ -96,7 +97,7 @@ def command(request):
myresponse=HttpResponse(json.dumps(my_dict),content_type='application/json',status=400)
return myresponse
else:
rtn_code,outs=IpNumGet(host,port,addr_pool_id)
rtn_code,outs=IpNumGet(host,port,addr_pool_id,index)
my_dict=dict()
if(rtn_code!=-1):
my_dict['response']=response_dic['success']
@@ -115,7 +116,7 @@ def command(request):
myresponse=HttpResponse(json.dumps(my_dict),content_type='application/json',status=400)
return myresponse
else:
rtn_code,outs=AllIpGet(host,port,addr_pool_id)
rtn_code,outs=AllIpGet(host,port,addr_pool_id,index)
my_dict=dict()
if(rtn_code!=-1):
my_dict['response']=response_dic['success']
@@ -536,10 +537,11 @@ def readconfig():
server_pwd=myconfig.get("server","server_pwd")
host=myconfig.get("redis","host")
port=myconfig.get("redis","port")
return server_pwd,host,port
index=myconfig.get("redis","index")
return server_pwd,host,port,index
def IpExist(host,port,candidate_ip):
P=Redis(host,port)
def IpExist(host,port,candidate_ip,index):
P=Redis(host,port,index)
rtn,outs=P.connect()
my_set=set()
if(rtn!=-1):
@@ -554,8 +556,8 @@ def IpExist(host,port,candidate_ip):
else:
return -1,outs
def IpNumGet(host,port,addr_pool_id):# IpNumGet(group_id)
P=Redis(host,port)
def IpNumGet(host,port,addr_pool_id,index):# IpNumGet(group_id)
P=Redis(host,port,index)
rtn,outs=P.connect()
my_set=set()
if(rtn!=-1):
@@ -568,8 +570,8 @@ def IpNumGet(host,port,addr_pool_id):# IpNumGet(group_id)
else:
return -1,outs
def AllIpGet(host,port,addr_pool_id):# AllIpGet(group_id)
P=Redis(host,port)
def AllIpGet(host,port,addr_pool_id,index):# AllIpGet(group_id)
P=Redis(host,port,index)
rtn,outs=P.connect()
my_set=set()
if(rtn!=-1):