def DelHandleDate(request): response = ujson.loads(request.body.decode('utf-8')) if response.get('prefix'): perfix = response.get('prefix') else: resp = {'status': 0, 'message': '输入正确的prefix'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') perfix_record = handles.objects.filter(perix=perfix) handle_record = reslove(perfix, ip=HANDLE_CONFIG['server'].get('ip'), port=HANDLE_CONFIG['server'].get('handleport')) if perfix_record.exists() or handle_record is not None: handle1 = handles.objects.get(perix=perfix) if response.get('index'): data = response.get('index') else: resp = {'status': 0, 'message': '输入正确的index'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') if 100 in data: resp = {'status': 0, 'message': 'index不能为100'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') result = daletedata(data, perfix, handle1.server.ip, handle1.server.port) if result is not 1: resp = {'status': 0, 'message': 'index ' + str(result) + '不存在'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') resp = {'status': 1, 'message': '删除成功'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') resp = {'status': 0, 'message': '前缀不存在无法删除'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8')
def AddHandleDate(request): response = ujson.loads(request.body.decode('utf-8')) if response.get('prefix'): perfix = response.get('prefix') else: resp = {'status': 0, 'message': '输入正确的prefix'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') perfix_record = handles.objects.filter(perix=perfix) handle_record = reslove(perfix, ip=HANDLE_CONFIG['server'].get('ip'), port=HANDLE_CONFIG['server'].get('handleport')) if perfix_record.exists() or handle_record is not None: handle1 = handles.objects.get(perix=perfix) if response.get('Data'): data = response.get('Data') else: resp = {'status': 0, 'message': '输入正确的data'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') record.index = [] record.type = [] record.value = [] errortype = ['HS_ADMIN', 'HS_SITE', 'HS_NA_DELEGATE', 'HS_SERV', 'HS_ALIAS', 'HS_PRIMARY', 'HS_VLIST'] for i in data: if re.match('^[1-9]\d*$', str(i.get('index'))) is None: resp = {'status': 0, 'message': 'index 应为数字'} return HttpResponse(ujson.dumps(resp)) if (i.get('type') in errortype): resp = {'status': 0, 'message': 'type 值错误'} return HttpResponse(ujson.dumps(resp)) if (i.get('index') in record.index): resp = {'status': 0, 'message': 'index 重复'} return HttpResponse(ujson.dumps(resp)) if (i.get('index') == '100'): resp = {'status': 0, 'message': 'index 不能为100'} return HttpResponse(ujson.dumps(resp)) record.index.append(i.get('index')) record.type.append(i.get('type')) record.value.append(i.get('data')) result = adddata(record, perfix, handle1.server.ip, handle1.server.port) # result = adddata(record, perfix, handle1.server.ip, handle1.server.port) if result is not 1: resp = {'status': 0, 'message': 'index ' + str(result) + '已经存在'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') resp = {'status': 1, 'message': '添加成功'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') resp = {'status': 0, 'message': '前缀不存在无法添加'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8')
def CreateHandle(request): response = ujson.loads(request.body.decode('utf-8')) data = response.get('Data') record.index = [] record.type = [] record.value = [] errortype = ['HS_ADMIN', 'HS_SITE', 'HS_NA_DELEGATE', 'HS_SERV', 'HS_ALIAS', 'HS_PRIMARY', 'HS_VLIST'] for i in data: if re.match('^[1-9]\d*$', str(i.get('index'))) is None: resp = {'status': 0, 'message': 'index 应为正整数'} return HttpResponse(ujson.dumps(resp)) if (i.get('type') in errortype): resp = {'status': 0, 'message': 'type 值错误'} return HttpResponse(ujson.dumps(resp)) if (i.get('index') in record.index): resp = {'status': 0, 'message': 'index 重复'} return HttpResponse(ujson.dumps(resp)) if (i.get('index') == '100'): resp = {'status': 0, 'message': 'index 不能为100'} return HttpResponse(ujson.dumps(resp)) record.index.append(i.get('index')) record.type.append(i.get('type')) record.value.append(i.get('data')) perfix = response.get('prefix') #HANDLE_CONFIG['server'].get('ip'), HANDLE_CONFIG['server'].get('sshport'), HANDLE_CONFIG['server'].get('username'), HANDLE_CONFIG['server'].get('password') handle_record = reslove(perfix, ip=HANDLE_CONFIG['server'].get('ip'), port=HANDLE_CONFIG['server'].get('handleport')) handle1 = handles.objects.filter(perix=perfix) if handle_record is not None or handle1.exists(): resp = {'status': 0, 'message': '该前缀已经存在'} return HttpResponse(ujson.dumps(resp)) now = django.utils.timezone.datetime.now().strftime('%Y-%m-%d') userid = response.get('userid') user1 = user.objects.get(id=userid) username = user1.username company = user1.company # serverid = response.get('serverid') server2 = server.objects.get(id=1) handle1 = handles.create(company=company, username=username, perix=perfix, count=0, time=now, server=server2) handle1.save() createh(record, perfix, HANDLE_CONFIG['server'].get('ip'), HANDLE_CONFIG['server'].get('handleport')) resp = {'status': 1, 'message': '创建成功'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8')
def OneQuery(request): data = ujson.loads(request.body.decode('utf-8')) perix = data.get('prefix') handle1 = handles.objects.get(perix=perix) handle_record = reslove(perix, ip=HANDLE_CONFIG['server'].get('ip'), port=HANDLE_CONFIG['server'].get('handleport')) handle1 = None handle1 = analyze_json(handle_record) for i in range(len(handle1.context)): handle1.context[i].pop('timestamp') if handle_record is not None: d1 = dict() d1['status'] = 1 data = list() i = 0 for row in handle1.context: if row.get('index') is not 100: i = i + 1 data.append(row) if data == []: resp = {'status': 0, 'message': "标识不存在"} return HttpResponse(ujson.dumps(resp)) d1['data'] = data return HttpResponse(ujson.dumps(d1))
def UpdateServer(request): response = ujson.loads(request.body.decode('utf-8')) prefix = response.get('prefix') handle = handles.objects.get(perix=prefix) handle_record = reslove(prefix, handle.server.ip, handle.server.port) handle1 = None handle1 = analyze_json(handle_record) handle1.context.pop() record.index = [] record.index = [] record.value = [] for i in range(len(handle1.context)): print i record.index.append(handle1.context[i].get('index')) record.type.append(handle1.context[i].get('type')) record.value.append(handle1.context[i].get('datas')) print record.value delete(prefix, handle.server.ip,handle.server.port) server2 = server.objects.get(id=1) handles.objects.filter(perix=prefix).update(server=server2) createh(record, prefix, HANDLE_CONFIG['server'].get('ip')) resp = {'status': 1, 'message': "修改成功"} return HttpResponse(ujson.dumps(resp))
def upload_file(request): if request.method == 'POST': userid = request.POST['userid'] user1 = user.objects.get(id=userid) username = user1.username company = user1.company # serverid = request.POST['serverid'] server2 = server.objects.get(id=1) fix2 = '20.500.12410/'+userid+'/' now = django.utils.timezone.datetime.now().strftime('%Y-%m-%d') uploadedFile = request.FILES.get('file', None) wb = xlrd.open_workbook(filename=uploadedFile.name, file_contents=request.FILES['file'].read()) current_date = django.utils.timezone.datetime.now() current_date_format = unicode(current_date.strftime('%Y-%m-%d %H-%M-%S')) destination = open( os.path.join("uploadexcel/" + current_date_format + "_" + username + "_" + uploadedFile.name), 'wb+') # 打开特定的文件进行二进制的写操作 for chunk in uploadedFile.chunks(): # 分块写入文件 destination.write(chunk) destination.close() succeedcreate = pd.DataFrame() error = pd.DataFrame() df = pd.read_excel(wb) if {'prefix', 'index', 'type', 'data'} <= set(list(df)): select_df = df[['prefix', 'index', 'type', 'data']] df2 = select_df.sort_values(by="prefix") # 存在空白单元格处理 havenul = df2[df2.isnull().T.any()] havenul['error'] = ('have null') error = havenul nonan_df = df2.dropna(axis=0, how='any') # index为100 index100 = nonan_df[nonan_df['index'].isin([100])] index100['error'] = 'index 100 invalid' error = error.append(index100) no_index100 = nonan_df[~nonan_df['index'].isin([100])] # 处理type类型错误 errortype = ['HS_ADMIN', 'HS_SITE', 'HS_NA_DELEGATE', 'HS_SERV', 'HS_ALIAS', 'HS_PRIMARY', 'HS_VLIST'] typeerror = no_index100[no_index100['type'].isin(errortype)] typeerror['error'] = 'type value have error ' error = error.append(typeerror) typetrue = no_index100[~no_index100['type'].isin(errortype)] # 处理数据相同行 same_df = typetrue[typetrue.duplicated()] same_df['error'] = ('have the same vlue , system have creat once succeed you don not neet to creat again') error = error.append(same_df) nosame_df = typetrue.drop_duplicates() group1 = nosame_df.groupby(nosame_df['prefix']) # 创建一个空的Dataframe errorprefix = pd.DataFrame() for a, b in group1: perfix = fix2 + str(a) perfix_record = handles.objects.filter(perix=perfix) handle_record = reslove(perfix, ip='172.171.1.80', port=8080) if perfix_record.exists() or handle_record is not None: b['error'] = 'prefix have existsed' errorprefix = errorprefix.append(b) else: sameindex = b[b.duplicated(subset=['index'], keep=False)] if sameindex.empty == False: sameindex['error'] = ('have the same index') error = error.append(sameindex) b.drop_duplicates(subset=['index'], keep=False, inplace=True) if b.empty == False: btype = b.astype('unicode') correct = btype[btype['index'].str.match('^[1-9]\d*$') == True] if correct.empty == False: record.index = correct['index'].values.tolist() record.type = correct['type'].values.tolist() record.value = correct['data'].values.tolist() handle1 = handles.create(company=company, username=username, perix=perfix, count=0, time=now, server=server2) handle1.save() createh(record, perfix, HANDLE_CONFIG['server'].get('ip'), HANDLE_CONFIG['server'].get('handleport')) succeedcreate = succeedcreate.append(correct) if correct.shape[0] != btype.shape[0]: errortype = btype[btype['index'].str.match('^[1-9]\d*$') == False] errortype['error'] = 'index value have error ' error = error.append(errortype) error = error.append(errorprefix) else: error[u'没有获得正确的列名'] = '' filename = "result.xlsx" writer = pd.ExcelWriter("uploadexcel/" + username + "_" + filename) error.to_excel(writer, sheet_name='ERROR') succeedcreate.to_excel(writer, sheet_name='SUCCESS') writer.save() file = open("uploadexcel/" + username + "_" + filename, 'rb') response = FileResponse(file) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment; filename=result.xlsx' return response
def Classifiedquery(request): data = ujson.loads(request.body.decode('utf-8')) if data.get('prefix'): biaoshi = data.get('prefix') else: resp = {'status': 0, 'message': '输入正确的prefix'} return HttpResponse(ujson.dumps(resp), content_type='application/json; charset=utf-8') handlepattern = '20.500.' niotpantter = 'cn.pub.xty.100' ecodepantter = '100036930100' oidpanntter = '1.2.156.86' gs1panntter = '[0-9]*' DNSpattern = '[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?' now = django.utils.timezone.datetime.now().strftime('%Y-%m-%d') str2='' for x in str(biaoshi): if x!=' ': str2=str2+x biaoshilist = ['10', '11', '20', '21', '22', '25', '27', '77', '44', '86'] biaoshilist2 = ['0.NA'] if re.search(handlepattern, biaoshi): handleperix = biaoshi obj1 = handles.objects.filter(perix=handleperix) if obj1.exists(): obj = handles.objects.get(perix=handleperix) obj.count += 1 obj.save() handle_record = reslove(handleperix, obj.server.ip, obj.server.port) else: handle_record = reslove(handleperix, ip=HANDLE_CONFIG['server'].get('ip'), port=HANDLE_CONFIG['server'].get('handleport')) handle1 = analyze_json(handle_record) d1 = dict() d1['status'] = 1 d1['type'] = 'handle' data = list() i = 0 for row in handle1.context: i = i + 1 data.append(row) # data.append(d1) if data == []: resp = {'status': 0, 'message': "标识不存在"} resolveRecord1 = resolveRecord.create( ip=HANDLE_CONFIG['server'].get('ip'), prefix=handleperix, success=0, time=now) resolveRecord1.save() return HttpResponse(ujson.dumps(resp)) else: d1['data'] = data # if obj1.exists(): # obj = handles.objects.get(perix=handleperix) # resolveRecord1 = resolveRecord.create(ip=obj.server.ip, prefix=handleperix, success=1, time=now) # resolveRecord1.save() # else: resolveRecord1 = resolveRecord.create( ip=HANDLE_CONFIG['server'].get('ip'), prefix=handleperix, success=1, time=now) resolveRecord1.save() return HttpResponse(ujson.dumps(d1)) if (re.search(niotpantter, biaoshi) != None): datalist = serverquery.OIDquery( HANDLE_CONFIG['server'].get('ip'), biaoshi) if datalist == {}: resp = {'status': 0, 'message': "不能解析该标识"} return HttpResponse(ujson.dumps(resp)) result = dict() result['type'] = 'niot' result['status'] = 1 result['data'] = [datalist] return HttpResponse(ujson.dumps(result)) if (re.search(ecodepantter, biaoshi) != None): datalist = serverquery.OIDquery( HANDLE_CONFIG['server'].get('ip'), biaoshi) if datalist == {}: resp = {'status': 0, 'message': "不能解析该标识"} return HttpResponse(ujson.dumps(resp)) result = dict() result['type'] = 'ecode' result['status'] = 1 result['data'] = [datalist] return HttpResponse(ujson.dumps(result)) if (re.search(oidpanntter, biaoshi) != None): datalist = serverquery.OIDquery(HANDLE_CONFIG['server'].get('ip'), biaoshi) if datalist == {}: resp = {'status': 0, 'message': "不能解析该标识"} return HttpResponse(ujson.dumps(resp)) result = dict() result['type'] = 'oid' result['status'] = 1 result['data'] = [datalist] return HttpResponse(ujson.dumps(result)) if str2[0:2] in biaoshilist or str2[0:4] in biaoshilist2: handle_record = reslove(str2, ip=HANDLE_CONFIG['server'].get('ip'), port=HANDLE_CONFIG['server'].get('handleport')) handle1 = analyze_json(handle_record) d1 = dict() d1['status'] = 1 d1['type'] = 'handle' data = list() i = 0 for row in handle1.context: i = i + 1 data.append(row) # data.append(d1) if data != []: d1['data'] = data resolveRecord1 = resolveRecord.create( ip=HANDLE_CONFIG['server'].get('ip'), prefix=biaoshi, success=1, time=now) resolveRecord1.save() return HttpResponse(ujson.dumps(d1)) if biaoshi.isalnum()==True : datalist = serverquery.GS1query(HANDLE_CONFIG['server'].get('ip'), biaoshi) if datalist == []: resp = {'status': 0, 'message': "不能解析该标识"} return HttpResponse(ujson.dumps(resp)) result = dict() result['status'] = 1 result['type'] = 'gs1' result['data'] = datalist return HttpResponse(ujson.dumps(result)) if re.match(DNSpattern, biaoshi) != None: datalist = serverquery.DNSquery( HANDLE_CONFIG['server'].get('ip'), biaoshi) if datalist == []: resp = {'status': 0, 'message': "不能解析该标识"} return HttpResponse(ujson.dumps(resp)) result = dict() result['status'] = 1 result['type'] = 'dns' result['data'] = datalist return HttpResponse(ujson.dumps(result)) resp = {'status': 0, 'message': "不能解析该标识"} return HttpResponse(ujson.dumps(resp))