def can_connect_vm_server(): ''' :return:{'content': 'success'} ''' try: resp = requests.get(flask_utils.vm_server(), verify=False, timeout=5) # resp = requests.get('https://10.100.11.199:5445',verify=False,timeout=5) return {'content': 'success'} except requests.ConnectionError as e: current_app.logger.error('无法连接虚机服务器{0}'.format(flask_utils.vm_server())) abort(408, '无法连接虚机服务器') except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500)
def get_or_post(api_route): try: path = api_route.replace('.', '/') if path == 'api/login': raise Exception('此方法不能用于登录') query_string = request.query_string.decode() if flask_utils.is_local_server: query_string = 'account={0}&password={1}&{2}'.format( Conf.get('USER', 'account'), Conf.get('USER', 'password'), query_string) else: query_string = 'account={0}&password={1}&{2}'.format( session['account'], session['password'], query_string) if request.method == 'GET': response = requests.get('{0}{1}?{2}'.format( flask_utils.vm_server(), path, query_string), verify=False) elif request.method == 'POST': response = requests.post('{0}{1}?{2}'.format( flask_utils.vm_server(), path, query_string), data=request.form.to_dict(), verify=False) else: raise Exception('目前只支持get和post两种方法') if response.status_code == 200: if path == 'api/EditPassword' and response.json( )['errorinfo'] == 'success': if flask_utils.is_local_server: Conf.set('USER', 'password', request.args['newPassword']) else: if flask_utils.is_local_server: Conf.set('USER', 'password', request.args['newPassword']) else: session['password'] = request.args['newPassword'] return json.loads(response.content.decode()) else: raise Exception(str(response.status_code)) except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) return {'content': e.message}, 500
def is_dynamic(): try: path = 'api/GetDynamicPassword' response = requests.get('{0}{1}'.format(flask_utils.vm_server(), path), verify=False) if response.status_code == 200: return {'content': response.json()['status']} else: raise Exception(response.status_code + ' api/GetDynamicPassword 失败') except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500)
def start_spice(configId = 0): data_dict = {} data_dict['account'] = Conf.get('USER', 'account') data_dict['password'] = Conf.get('USER', 'password') data_dict['mac'] = dev_info.get_mac_address() data_dict['systemType'] = dev_info.get_system_type()[0] #data_dict['systemName'] = dev_info.get_system_type()[1] data_dict['configId'] = configId response = requests.get('{0}{1}'.format(flask_utils.vm_server(), 'api/ServerSPICEConsole'), data_dict, verify=False) if response.status_code==200: content = json.loads(response.content.decode()) if content['status']== 'ACTIVE': return content raise Exception('status:%s,%s' % (content['status'],content['status_zh']))
def login(): try: path = 'api/login' query_string = request.query_string.decode() response = requests.get('{0}/{1}?{2}'.format(flask_utils.vm_server(), path, query_string), verify=False) if not response.status_code == 200: raise Exception(str(response.status_code) + ':' + response.json()['errorinfo']) if response.json()['errorinfo'] == 'success': resp = Response(json.dumps({'content': response.json()['errorinfo']}), mimetype='application/json', status=200) if flask_utils.is_local_server: Conf.set('USER', 'account', request.args.to_dict()['account']) Conf.set('USER', 'password', request.args.to_dict()['password']) Conf.set('USER', 'dynamicPass', request.args.to_dict()['dynamicPass']) Conf.set('USER', 'remember_pass', request.args.to_dict()['remember_pass']) Conf.set('USER', 'auto_login', request.args.to_dict()['auto_login']) Conf.set('USER', 'is_login', 'true') # session.permanent=False # session['account'] = request.args.to_dict()['account'] else: session['account'] = request.args.to_dict()['account'] session['password'] = request.args.to_dict()['password'] session['dynamicPass'] = request.args.to_dict()['dynamicPass'] session['remember_pass'] = request.args.to_dict()['remember_pass'] session['auto_login'] = request.args.to_dict()['auto_login'] if request.args.to_dict()['remember_pass'] == 'false': resp.set_cookie('account', request.args.to_dict()['account']) if request.args.to_dict()['remember_pass'] == 'true': resp.set_cookie('account', request.args.to_dict()['account'], 3600 * 24 * 30) # 如果记住密码,则保存一个月 resp.headers.extend(flask_utils.Cross_Origin_Headers(request)) return resp else: abort(400,response.json()['errorinfo']) except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500)