def servers_info_extract(req): rep = BaseResponse() # 创建 回调类对象 user_info = req.session['user_info'] # 从session中拿取 用户ID u_nid = user_info['nid'] # 拿取该表对象 ser_obj = models.PertestingServersTable.objects.get(user_info_id=u_nid) # 实例化服务器对象 ser = servers(ser_obj.server_add, ser_obj.server_port, ser_obj.server_username, ser_obj.server_password) try: res_mem, res_sys = ser.info_dispose() # 计算内存占用比 ser_obj.memory_used_per = (int(res_mem['used']) / int(res_mem['total'])) * 100 # 计算系统负债的情况 ser_obj.sysLoad_time = res_sys[0] ser_obj.sysLoad_runTime = res_sys[2] + res_sys[3].replace(",", "") ser_obj.sysLoad_userNum = res_sys[5].replace(",", "") ser_obj.sysLoad_loadLevel_1min = res_sys[9].replace(",", "") ser_obj.sysLoad_loadLevel_5min = res_sys[10].replace(",", "") ser_obj.sysLoad_loadLevel_15min = res_sys[11].replace("\n", "") ser_obj.save() rep.status = True rep.data = [ ser_obj.sysLoad_time, ser_obj.sysLoad_runTime, ser_obj.sysLoad_userNum ] rep.message = [ ser_obj.memory_used_per, ser_obj.sysLoad_loadLevel_1min, ser_obj.sysLoad_loadLevel_5min, ser_obj.sysLoad_loadLevel_15min ] except Exception as e: print('错误内容:', e) rep.status = False rep.data = [] rep.message = [] rep.summary = '运行错误!servers_info_extract 接口请求失败。' finally: ser.server_linke_close() return HttpResponse(json.dumps(rep.__dict__))
def post(self, *args, **kwargs): rep = BaseResponse() try: file_metas = self.request.files["img"] for meta in file_metas: file_name = meta['filename'] file_path = os.path.join('statics', 'upload', commons.generate_md5(file_name)) with open(file_path, 'wb') as up: up.write(meta['body']) rep.status = True rep.data = file_path except Exception as ex: rep.summary = str(ex) self.write(json.dumps(rep.__dict__))
def post(self, *args, **kwargs): print("我到了!!!") rep = BaseResponse() try: file_metas = self.request.files["fafafa"] for meta in file_metas: file_name = meta['filename'] xxx = file_name.split(".")[-1] username = self.session['user_info']["username"] file_path = os.path.join('statics', 'upload', username, 'temp', commons.generate_md5(file_name) + '.' + xxx) with open(file_path, 'wb') as up: up.write(meta['body']) rep.status = True rep.data = file_path except Exception as ex: rep.summary = str(ex) self.write(json.dumps(rep.__dict__))
def post(self, *args, **kwargs): rep = BaseResponse() form = CommentForm() if form.valid(self): form._value_dict['ctime'] = datetime.datetime.now() conn = ORM.session() obj = ORM.Comment(user_info_id=self.session['user_info']['nid'], news_id=form._value_dict['news_id'], reply_id=form._value_dict['reply_id'], content=form._value_dict['content'], up=0, down=0, ctime=datetime.datetime.now()) conn.add(obj) # 获取nid conn.flush() conn.refresh(obj) rep.data = { 'user_info_id': self.session['user_info']['nid'], 'username': self.session['user_info']['username'], 'nid': obj.nid, 'news_id': obj.news_id, 'ctime': obj.ctime.strftime("%Y-%m-%d %H:%M:%S"), 'reply_id': obj.reply_id, 'content': obj.content, } # 将评论数+1 conn.query(ORM.News).filter( ORM.News.nid == form._value_dict['news_id']).update( {"comment_count": ORM.News.comment_count + 1}, synchronize_session="evaluate") conn.commit() conn.close() rep.status = True else: rep.message = form._error_dict print(rep.__dict__) self.write(json.dumps(rep.__dict__))
def upload_image(request): """ 上传图片 :param request: :return: """ rep = BaseResponse() try: obj = request.FILES.get('img') file_path = os.path.join('statics', 'upload', commons.generate_md5(obj.name)) f = open(file_path, 'wb') for chunk in obj.chunks(): f.write(chunk) f.close() rep.status = True rep.data = file_path except Exception as ex: rep.summary = str(ex) return HttpResponse(json.dumps(rep.__dict__))
def per_data_extract(req): """ 前端页面点击发送请求 后 执行的操作 :param req: :return: """ rep = BaseResponse() # 创建 回调类对象 user_info = req.session['user_info'] # 从session中拿取 用户ID u_nid = user_info['nid'] # 拿取该表对象 per_obj = models.PertestingTable.objects.get(user_info_id=u_nid) # 将部分数据进行处理 协议类型\并发数\请求头\请求体类型转换 url = per_obj.hosts + per_obj.paths if not url.find('https'): flag = False else: flag = True # 查询到最大并发数 与 每次启动的并发数 并且进行计算 maxNum = int(per_obj.maxnum) onceNum = int(per_obj.oncenum) if onceNum < maxNum: per_obj.oncenum += onceNum else: per_obj.oncenum = maxNum # print('此时的并发数为:',onceNum) # 请求头进行预处理 try: headers = json.loads(per_obj.headers) except Exception: headers = {"Content-Type": "application/json"} # 请求体进行预处理 try: datas = json.loads(per_obj.datas) except Exception: datas = '' # 请求断言进行预处理 try: assert_dic = json.loads(per_obj.assert_dic) # 对断言数据进行预处理 except Exception: assert_dic = {} maxTime = per_obj.maxTime # 接口响应的最大时间 # 将表中的数据 初始化到 数据统计类中 ds = DataStatistics(method=per_obj.method, url=url, headers=headers, data=datas, count=onceNum, flag=flag, assert_dic=assert_dic, maxTime=maxTime) if req.method == 'POST': loop_num = req.POST.get('loop_num') # 收集前端请求中的循环次数 per_obj.loop_num += int(loop_num) # 进行累加求值 try: ds.together_send() # 计算出来的指标 rep.status = True data = ds.data_response() # 提取指标 # 数据存储与计算 per_obj.success_req += data[0] # 成功请求数 per_obj.lose_req += data[1] # 失败请求数 per_obj.total_req += data[0] + data[1] # 总请求数 per_obj.total_time += data[2] # 总共花销的时间 per_obj.avg_time = per_obj.total_time / per_obj.loop_num # 平均花销的时间 # 以下数据 属于图表使用 per_obj.count_time = data[3] # 每次请求用时 per_obj.rps = onceNum / data[3] # 每次的并发数 / 每次请求用时 # 保存数据入库 per_obj.save() # 获取当前的系统时间 lt = '{0}{1}:{2}:{3}' localtime = time.localtime() if localtime.tm_hour < 12: lti = lt.format('上午', localtime.tm_hour, localtime.tm_min, localtime.tm_sec) elif 12 <= localtime.tm_hour < 14: lti = lt.format('中午', localtime.tm_hour, localtime.tm_min, localtime.tm_sec) else: lti = lt.format('下午', localtime.tm_hour, localtime.tm_min, localtime.tm_sec) # 将数据存入 前端回调类对象中(这里存在读写消耗) rep.data = [ per_obj.success_req, per_obj.lose_req, per_obj.total_req, per_obj.avg_time, per_obj.total_time, per_obj.oncenum ] rep.message = [per_obj.count_time, per_obj.rps, lti] # print(rep.data) rep.summary = '运行ing...' except (IndexError): rep.status = False rep.data = [] rep.summary = '运行错误!per_data_extract 接口请求失败。' return HttpResponse(json.dumps(rep.__dict__))
def update_item(req): ''' 1. 初始化需要爬虫的准备数据 2.更新项目 使用get请求 ,爬取项目相关数据 用post请求 :param req: :return: ''' rep = BaseResponse() # 旧禅道 爬虫信息初始化 login_url = r'http://it.bbdservice.com:8280/zentao/user-login.html' pro_url = r'http://it.bbdservice.com:8280/zentao/product-all-0-noclosed-order_desc-13-100-1.html' bug_url = r'http://it.bbdservice.com:8280/zentao/bug-browse-%s-0-all-0--598-500-1.html' user_info = req.session['user_info'] u_nid = user_info['nid'] user_obj = models.UserInfo.objects.get(nid=u_nid) username = user_obj.username password = user_obj.password num = req.POST.get('itemId') # 获取用户选择的项目ID # 爬虫信息初始化 ss = SpiderBugInfo(username, password, num, login_url, pro_url, bug_url) # 数据库信息初始化 mbi = MysqlBugInfo(num) if req.method == 'GET': zentao_id = req.GET.get('zentao_id') # 获取用户选择 禅道版本ID号 print("zentao_id_get:", zentao_id) # 根据不同的 禅道版本选择 不同的方式获取项目信息 if zentao_id == '1': try: pro_item_1 = ss.product_info() models.SpiderProduct.objects.create(pro_info=pro_item_1) rep.status = True rep.data = pro_item_1 rep.message = "数据成功获取!" except Exception as e: print('报错信息:', e) rep.status = False rep.message = "爬取数据,出错!" elif zentao_id == '2': try: pro_item_2 = mbi.product_info() models.SpiderProduct.objects.create(pro_info=pro_item_2) rep.status = True rep.data = pro_item_2 rep.message = "数据成功获取!" except Exception as e: print('报错信息:', e) rep.status = False rep.message = "远程数据库,拒绝访问!" else: rep.status = False rep.message = "无项目信息!~" if req.method == "POST": models_obj = models.SpiderTable.objects.get(user_info_id=u_nid) zentao_id = req.POST.get('zentao_id') # 获取用户选择 禅道版本ID号 print("zentao_id_post:", zentao_id) # 根据不同的 禅道版本选择 不同的方式获取BUG信息 if zentao_id == '1': try: bug_lis, not_close = ss.bug_info() models_obj.bug_info_close = bug_lis models_obj.bug_info_not_close = not_close models_obj.save() rep.status = True rep.message = '数据爬取成功' except IndexError as e: print("报错信息:", e) rep.message = '请选择项目 或 该项目内容为空!' rep.status = False elif zentao_id == '2': try: bug_lis, not_close = mbi.bug_info() models_obj.bug_info_close = bug_lis models_obj.bug_info_not_close = not_close models_obj.save() rep.status = True rep.message = '数据爬取成功' except IndexError as e: print("报错信息:", e) rep.message = '锅佬倌,点下拉框选项目!' rep.status = False else: rep.status = False rep.message = "无BUG信息!~" # print("bug_lis",bug_lis) # print("not_close",not_close) return HttpResponse(json.dumps(rep.__dict__))