def get_response_from_exception(excep, exception_mapping): status = exception_mapping.get(excep.__class__, None) if status is None: status = 500 ret = HTTPResponse(status=status, body=str(excep)) ret.add_header("Content-Type", "text/plain") return ret
def postTest(): var1 = request.forms.get('prm1') var2 = request.forms.get('prm2') retBody = 'Hello Post ' + var1 + ', ' + var2 r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'text/html') return r
def kml_index(self): session = self.beaker print 'kml_index' err, suc = [], [] kml_layer_name = '' with self.session() as s: try: if request.method == 'POST': csrf = request.params.get('csrf', '') if session.get('csrf', '') != csrf: print 'csrf token mismatch' r = HTTPResponse(status=302) r.set_header('Location: /kml/') raise r print 'method = POST' name = request.params.get('name', '') name = name.decode('utf-8') upload = request.files.get('kml_file', '') if not name: err.append(u'KMLレイヤーの名前を入力してください') else: kml_layer_name = name if not upload: err.append(u'ファイルがアップロードされていません') if not err: tmp_fname = '/tmp/minarepo_kml_%s' % random_str(40) upload.save(tmp_fname) try: with open(tmp_fname, 'rb') as fh: fdata = fh.read() geo_layer = GeoLayer( name=name, content=fdata, file_size=len(fdata) ) s.add(geo_layer) finally: os.remove(tmp_fname) suc.append(u'KMLレイヤーを作成しました: %s' % name) kml_layer_name = '' layers = s.query(GeoLayer).order_by(GeoLayer.created.desc()) layers = [ l.to_api_dict() for l in layers ] except: s.rollback() raise else: s.commit() print 'commited' session['csrf'] = random_str(100) return self._render('kml.html.j2', err=err, suc=suc, layers=layers, kml_layer_name=kml_layer_name, session=self.beaker)
def patch(self, id=None): if id: m = self.query().get(id) if m: for key, value in self.model.from_dict(request.json, self.db).items(): # if value being replaced is a model or list of models, # delete it/them from the database first if isinstance(value, list): old = getattr(m, key) newitems = [i for i in value if i not in old] olditems = [i for i in old if i not in value] for item in olditems: old.remove(item) if issubclass(type(item), Association): self.db.delete(item) for item in newitems: old.append(item) else: setattr(m, key, value) self.db.commit() self.db.refresh(m) response = HTTPResponse(status=200, body=json.dumps(m.to_dict())) response.content_type = "application/json" return response abort(404)
def default_error_handler(error): if isinstance(error, HTTPError): r = HTTPResponse() error.apply(r) r.content_type = error.content_type return r return error
def getTest(): print request var1 = request.query.prm1 var2 = request.query.prm2 retBody = 'Hello Get ' + var1 + ', ' + var2 r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'text/html') return r
def auth_fail(self, gss_context=None): resp = HTTPResponse(self._fail_response, status=401) resp.set_header('Content-Type', self._content_type) resp.set_header( 'WWW-Authenticate', 'Negotiate' + (' ' + gss_context if gss_context else '') ) return resp
def method_not_allowed(res): if request.method == 'OPTIONS': new_res = HTTPResponse() new_res.set_header('Access-Control-Allow-Origin', '*') new_res.set_header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE') return new_res res.headers['Allow'] += ', OPTIONS' return request.app.default_error_handler(res)
def success(): response = HTTPResponse() response.status = 200 response.body = json.dumps( { 'message': 'Success' } ) + "\n" return response
def method_not_allowed(res): if request.method == 'OPTIONS': new_res = HTTPResponse() new_res.headers['Access-Control-Allow-Origin'] = '*' new_res.headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT' new_res.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept' return new_res res.headers['Allow'] += ', OPTIONS' return request.app.default_error_handler(res)
def postTestJson(): var = request.json retBody = { "ret": "ok", "retPrm": "XX " + var["prm1"] + var["prm2"], } r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'application/json') return r
def apikeyNotValid(): response = HTTPResponse() response.status = 200 response.body = json.dumps( { 'message': 'api key not valid', } ) + "\n" return response
def badRequest(key): response = HTTPResponse() response.status = 400 response.body = json.dumps( { 'message': 'Failed', 'BadRequest': key } ) + "\n" return response
def get(self, id=None): if id: match = self.query().get(id).to_dict() if match: return json.dumps(match) abort(404) else: response = HTTPResponse(status=200, body=json.dumps([m.to_dict() for m in self.query().all()])) response.content_type = "application/json" return response
def make_error(message): formats = { 'json': 'application/json', 'xml': 'application/xml', 'jsonp': 'application/javascript', } format_ = bottle.request.query.get('format', 'json') response = HTTPResponse(status=message, content_type=formats[format_]) response.headers['Access-Control-Allow-Origin'] = '*' return response
def cannotTweet(): response = HTTPResponse() response.status = 500 response.body = json.dumps( { 'message': 'Failed', 'Error': 'CannotTweet' } ) + "\n" return response
def rotaryEncoderIf(): var = request.json # print (var) rotate = rotaryEncoder() retBody = { "ret": "ok", "rotate": rotate } r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'application/json') return r
def tapIf(): var = request.json # print (var) tapNum = tap() retBody = { "ret": "ok", "tapNum": tapNum } r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'application/json') return r
def get_entries(): # エントリーの一覧 entries = [] # エントリーの一覧をファイルから取得 entries_file = config.get('alarm', 'entries_json_path') with open(entries_file, 'r') as f: entries = json.load(f) # JSONデータにして返却 r = HTTPResponse(status=200, body=json.dumps(entries)) r.set_header('Content-Type', 'application/json') return r
def index(data): data_array = numpy.array(data.split('_'),dtype=numpy.float) # 機械学習結果のモデルを読み込み clf = joblib.load('./clf/sample01.pkl') # 実行 result = clf.predict([data_array]) r = HTTPResponse(sattus=200, body = '<h1>%d</h1>' % result) r.set_header('Access-Control-Allow-Origin','*'); return r
def buttonIf(): var = request.json # print (var) index = int(var["index"]) onoff = button(index) retBody = { "ret": "ok", "onoff": "on" if onoff == True else "off" } r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'application/json') return r
def GSensorIf(): var = request.json # print (var) xyz = GSensor() retBody = { "ret": "ok", "x": xyz[0], "y": xyz[1], "z": xyz[2], } r = HTTPResponse(status=200, body=retBody) r.set_header('Content-Type', 'application/json') return r
def return_analysis_result(): """ Example of analyzer API(GET) """ # Get submitted contents. text = request.query.input_text # Analyze. _text = your_tool.lowercase(text) # Make a request. body = json.dumps(_text) r = HTTPResponse(status=200, body=body) r.set_header("Content-Type", "application/json") return r
def send_response(code, message=None): r = HTTPResponse(status=code) r.add_header('Access-Control-Allow-Origin', '*') r.set_header('Content-Type', 'application/json') if message is None: r.set_header('Content-Type', 'text/plain') elif type(message) is str: e = {'error': message} r.body = json.dumps(e) elif type(message) is dict: r.body = json.dumps(message) else: r.body = message return r
def open_image(file_path): image = Image.open(file_path) # カラーモードを変換する image = image.convert("RGBA") # 更新をbufferにpngで保存 buf = StringIO() image.save(buf, "png") # bufferからレスポンス作成 response = HTTPResponse(status=200, body=buf.getvalue()) response.set_header("Content-type", "Image") return response
def index(): textdata = request.query.textdata print textdata data = {"title": textdata} sr = analyze(data) dictionary = [] for r in sr: dictionary_data = {"posts_id": r.id, "score": r.score} dictionary.append(dictionary_data) print r.id, "with score", r.score body = json.dumps(dictionary) r = HTTPResponse(status=200, body=body) r.set_header("Content-Type", "application/json") return r
def parse_authorization_value(self, header_value): """Parse header_value and return kwargs to apply bottle method parameters""" try: if not header_value: username = None else: # Check if its valid obtaining the password_timestamp username = self.manager.get_user(token=header_value) except Exception as exc: # Check if resp = HTTPResponse("Wrong JWT token!", "401 Unauthorized") resp.set_header("Content-Type", "text/plain") raise resp return {self.keyword: username}
def result(): upload = request.files.get('upfile') upload.save("./image", overwrite=True) feats = cpp_exec(upload.filename) if feats.size == 0: body = {} for i in xrange(categorySize): body[i] = 0 else: feats = feats.reshape(featsDim, 1) body = {} for i in xrange(categorySize): body[i] = dataID[i][np.argmax(dataSet[categoryIndex[i][0]:categoryIndex[i][1]].dot(feats))] r = HTTPResponse(status=200, body=json.dumps(body)) r.set_header('Content-Type', 'text/plain') return r
def error404(error): mheader = request.get_header("host") output = call_lua("./src/static.lua", mheader, request.path) resp = HTTPResponse(body=output, status=200) mtype = "application/octet-stream" lowered = request.path.lower() if lowered.endswith("jpg") or lowered.endswith("jpeg"): mtype = "image/jpeg" elif lowered.endswith("gif"): mtype = "image/gif" elif lowered.endswith("png"): mtype = "image/png" elif lowered.endswith("webm"): mtype = "video/webm" resp.content_type = mtype return resp
def dropbox_auth(): """ Get Dropbox oatuh tokens """ storage = SettingStorage(SETTINGS_FILE) consumer_key = storage.get('dropbox:consumer_key') consumer_secret = storage.get('dropbox:consumer_secret') sess = session.DropboxSession(consumer_key, consumer_secret, 'app_folder') request_token = sess.obtain_request_token() callback = "%s://%s/dropbox/auth/callback" % (request.urlparts.scheme, request.urlparts.netloc) auth_url = sess.build_authorize_url(request_token, callback) # response.set_cookie('request_token', '&'.join([request_token.key, request_token.secret])) resp = HTTPResponse("", status=302, Location=auth_url) resp.set_cookie('request_token', '&'.join([request_token.key, request_token.secret])) return resp
def end_response(): return HTTPResponse(status=200)
def health(): status = subprocess.check_output(["sv", "status", "prometheus"]) if status.startswith("run: prometheus:"): return "Prometheus is running\n" else: return HTTPResponse(body='Prometheus is not running\n', status=500)
def create_response(body, status=200, err=None): body['err'] = err r = HTTPResponse(status=status, body=body) r.set_header('Content-Type', 'application/json') return r
def ota(): headers = request.headers for k in headers: logging.debug("header " + k + ' = ' + headers[k]) try: if '->' in headers.get('X-Esp8266-Version', None): # X-Esp8266-Version = [email protected]>[email protected] device, f = headers.get('X-Esp8266-Version', None).split('=') firmware_name, have_version, want_version = f.split('@') else: # X-Esp8266-Version = cf3a07e0=h-sensor=1.0.1=1.0.2 device, firmware_name, have_version, want_version = headers.get( 'X-Esp8266-Version', None).split('=') except: raise logging.warn("Can't find X-Esp8266-Version in headers; returning 403") return HTTPResponse(status=403, body="Not permitted") # Record additional detains in DB if device not in db: db[device] = {} db[device]['mac'] = headers.get('X-Esp8266-Ap-Mac', None) db[device]['free_space'] = headers.get('X-Esp8266-Free-Space', None) db[device]['chip_size'] = headers.get('X-Esp8266-Chip-Size', None) db[device]['sketch_size'] = headers.get('X-Esp8266-Sketch-Size', None) logging.info("Homie firmware=%s, have=%s, want=%s on device=%s" % (firmware_name, have_version, want_version, device)) # if the want_version contains the special '@' separator then # this is a request from ourselves with both fw_name and # fw_version included - allowing for fw changes fw_file = None if '@' in want_version: fw = scan_firmware() for fw_key in fw: firmware = "%s@%s" % (fw[fw_key]['firmware'], fw[fw_key]['version']) if generate_ota_payload(firmware) == want_version: fw_file = fw[fw_key]['filename'] break else: fw_name = firmware_name fw_version = want_version fw_file = "%s-%s.bin" % (fw_name, fw_version) if fw_file is None: logging.warn( "Firmware not found, %s does not match any firmware in our list; returning 304" % (want_version)) return HTTPResponse(status=304, body="OTA aborted, firmware not found") fw_path = os.path.join(OTA_FIRMWARE_ROOT, fw_file) if not os.path.exists(fw_path): logging.warn("%s not found; returning 304" % (fw_path)) return HTTPResponse(status=304, body="OTA aborted, firmware not found") # check free space vs .bin file on disk and refuse stat = os.stat(fw_path) fw_size = stat.st_size try: free_space = headers.get('X-Esp8266-Free-Space', None) if free_space and free_space < fw_size: logging.warn( "Firmware too big, %d free on device but binary is %d; returning 304" % (free_space, fw_size)) return HTTPResponse( status=304, body="OTA aborted, not enough free space on device") except: logging.warn( "Can't find X-Esp8266-Free-Space in headers; skipping size checks") logging.info("Returning OTA firmware %s" % (fw_path)) return static_file(fw_file, root=OTA_FIRMWARE_ROOT)
def generate_steps(model, key, token, company, **arts): '''Gets an environment model, key, status token and a dict of artifacts. The arts dict looks like this: {'art_type': {'file_path': string, 'filename': string, 'type': string}} Returns a scenario or None if error occured. A scenario is a list of steps. Each step has one or more function(s) with argument(s). The steps are executed in sequence, but the functions in each step are executed in parallel.''' steps = [] if 'order' in model.keys(): for step in model['order']: if type(step) == unicode: # Add stuff for this artifact art_type = step if art_type in arts: steps.append(gen_copy_steps(model, key, token, arts[art_type])) script_steps = gen_script_steps(model, key, token, arts[art_type], company) if script_steps: for s_type in script_steps: steps.append(script_steps[s_type]) elif type(step) == list: # Add stuff for all the arts in the list copy_step = [] for art_type in step: if art_type in arts: copy_step = copy_step + gen_copy_steps(model, key, token, arts[art_type]) if len(copy_step) > 0: steps.append(copy_step) script_step = [] for art_type in step: if art_type in arts: script_steps = gen_script_steps(model, key, token, arts[art_type], company) if script_steps: script_step.append(script_steps) # ## In a special case where the list has only 1 member we ## emulate the 'unicode' behaviour # if len(script_step) == 1: script_steps = script_step[0] for s_type in script_steps: steps.append(script_steps[s_type]) steps = steps + script_step else: # Adding the same script types to previous steps. unified_dict = {} for phase in script_step: for s_type in phase: if s_type not in unified_dict: unified_dict[s_type] = phase[s_type] else: unified_dict[s_type] = (unified_dict[s_type] + phase[s_type]) # Adding the keys in right order for s_type in scripts_order: if s_type in unified_dict: steps.append(unified_dict[s_type]) #pprint.pprint (unified_dict, indent=4) elif type(step) == dict: # Add only for the given art-node pairs art_type = step.keys()[0] if art_type in arts: steps.append(gen_copy_steps(model, key, token, arts[art_type], step[art_type])) script_steps = gen_script_steps(model, key, token, arts[art_type], company, step[art_type]) if script_steps: for s_type in script_steps: steps.append(script_steps[s_type]) else: raise HTTPResponse("Don't know what to do for %s in order.\n" % type(step), 400) else: # A model without order attribute step = [] for art in model['arts_to_nodes']: if art in arts: for node in model['arts_to_nodes'][art]: step.append([copy_art, arts[art], node, key, token]) steps.append(step) if 'arts_to_scripts' in model.keys(): script_step = [] for art in model['arts_to_nodes']: if art in arts: script_steps = gen_script_steps(model, key, token, arts[art], company) if script_steps: script_step.append(script_steps) if len(script_step) == 1: script_steps = script_step[0] for s_type in script_steps: steps.append(script_steps[s_type]) else: # Adding the same script types to previous steps. unified_dict = {} for phase in script_step: for s_type in phase: if s_type not in unified_dict: unified_dict[s_type] = phase[s_type] else: unified_dict[s_type] = (unified_dict[s_type] + phase[s_type]) # Adding the keys in right order for s_type in scripts_order: if s_type in unified_dict: steps.append(unified_dict[s_type]) return steps
def do_proxy(self, url): info = self.browser_mgr.init_remote_browser_session() if not info: return self._raise_error(400, 'invalid_connection_source') try: kwargs = info user = info['the_user'] collection = info['collection'] recording = info['recording'] if kwargs['type'] == 'replay-coll': collection.sync_coll_index(exists=False, do_async=False) url = self.add_query(url) kwargs['url'] = url wb_url = kwargs.get('request_ts', '') + 'bn_/' + url request.environ['webrec.template_params'] = kwargs request.environ['pywb.static_prefix'] = self.BUNDLE_PREFIX remote_ip = info.get('remote_ip') if remote_ip and info['type'] in self.MODIFY_MODES: remote_ip = self.check_rate_limit(user, remote_ip) kwargs['ip'] = remote_ip resp = self.render_content(wb_url, kwargs, request.environ) resp = HTTPResponse(body=resp.body, status=resp.status_headers.statusline, headers=resp.status_headers.headers) return resp except Exception as e: import traceback traceback.print_exc() @self.jinja2_view('content_error.html') def handle_error(status_code, err_body, environ): response.status = status_code kwargs['url'] = url kwargs['status'] = status_code kwargs['err_body'] = err_body kwargs['host_prefix'] = self.get_host_prefix(environ) kwargs['proxy_magic'] = environ.get('wsgiprox.proxy_host', '') return kwargs status_code = 500 if hasattr(e, 'status_code'): status_code = e.status_code if hasattr(e, 'body'): err_body = e.body elif hasattr(e, 'msg'): err_body = e.msg else: err_body = '' return handle_error(status_code, err_body, request.environ)
def get_off(self): print("Received manual pump off.") self.board.pump_off() return HTTPResponse(status=200)
def error_field(cls, error_validate_schema): return HTTPResponse(json.dumps({"Error": error_validate_schema.args}), 422, HEADERS_RESPONSE)
def error_get_cart(cls): raise HTTPResponse(json.dumps({"Error": "No cart was found."}), 404, HEADERS_RESPONSE)
def error_validation_uuid(cls): return HTTPResponse(json.dumps({"Error": "Invalid UUID."}), 422, HEADERS_RESPONSE)
def error_payload(cls): return HTTPResponse(json.dumps({"Error": "Invalid Payload."}), 422, HEADERS_RESPONSE)
def success_put(cls): return HTTPResponse({"Success": 'The cart was updated.'}, 200, HEADERS_RESPONSE)
def success_list(cls, result_object): return HTTPResponse({"objects": json.loads(result_object)}, 200, HEADERS_RESPONSE)
def ping_response(): return HTTPResponse(status=200)
def index(): status_code = 200 return HTTPResponse(status=status_code)
def error_invalid_user_session(cls): return HTTPResponse(json.dumps({"Error": "Invalid user session."}), 404, HEADERS_RESPONSE)
def put_schedule(self): print("Received updated pump schedule.") self.schedule = request.json return HTTPResponse(status=200)
def success_create(cls, created_id): return HTTPResponse({'created_id': str(created_id)}, 201, HEADERS_RESPONSE)
def handle_routing(self, wb_url, user, coll_name, rec_name, type, is_embed=False, is_display=False, sources='', inv_sources='', redir_route=None): wb_url = self._full_url(wb_url) if user == '_new' and redir_route: return self.do_create_new_and_redir(coll_name, rec_name, wb_url, redir_route) sesh = self.get_session() remote_ip = None frontend_cache_header = None patch_recording = None the_user, collection, recording = self.user_manager.get_user_coll_rec( user, coll_name, rec_name) if not the_user: msg = 'not_found' if user == 'api' else 'no_such_user' self._raise_error(404, msg) coll = collection.my_id if collection else None rec = recording.my_id if recording else None if type in self.MODIFY_MODES: if sesh.is_new() and self.is_content_request(): self.redir_set_session() if not recording: self._redir_if_sanitized(self.sanitize_title(rec_name), rec_name, wb_url) # don't auto create recording for inner frame w/o accessing outer frame self._raise_error(404, 'no_such_recording') elif not recording.is_open(): # force creation of new recording as this one is closed self._raise_error(400, 'recording_not_open') collection.access.assert_can_write_coll(collection) if the_user.is_out_of_space(): self._raise_error(402, 'out_of_space') remote_ip = self._get_remote_ip() remote_ip = self.check_rate_limit(the_user, remote_ip) if inv_sources and inv_sources != '*': #patch_rec_name = self.patch_of_name(rec, True) patch_recording = recording.get_patch_recording() #patch_recording = collection.get_recording_by_name(patch_rec_name) if type in ('replay-coll', 'replay'): if not collection: self._redir_if_sanitized(self.sanitize_title(coll_name), coll_name, wb_url) if sesh.is_new() and self.is_content_request(): self.redir_set_session() else: self._raise_error(404, 'no_such_collection') access = self.access.check_read_access_public(collection) if not access: if sesh.is_new() and self.is_content_request(): self.redir_set_session() else: self._raise_error(404, 'no_such_collection') if access != 'public': frontend_cache_header = ('Cache-Control', 'private') if type == 'replay': if not recording: self._raise_error(404, 'no_such_recording') request.environ['SCRIPT_NAME'] = quote(request.environ['SCRIPT_NAME'], safe='/:') wb_url = self._context_massage(wb_url) wb_url_obj = WbUrl(wb_url) is_top_frame = (wb_url_obj.mod == self.frame_mod or wb_url_obj.mod.startswith('$br:')) if type == 'record' and is_top_frame: result = self.check_remote_archive(wb_url, type, wb_url_obj) if result: mode, wb_url = result new_url = '/{user}/{coll}/{rec}/{mode}/{url}'.format( user=user, coll=coll_name, rec=rec_name, mode=mode, url=wb_url) return self.redirect(new_url) elif type == 'replay-coll' and not is_top_frame: collection.sync_coll_index(exists=False, do_async=False) kwargs = dict( user=user, id=sesh.get_id(), coll=coll, rec=rec, coll_name=quote(coll_name), rec_name=quote(rec_name, safe='/*'), the_user=the_user, collection=collection, recording=recording, patch_recording=patch_recording, type=type, sources=sources, inv_sources=inv_sources, patch_rec=patch_recording.my_id if patch_recording else None, ip=remote_ip, is_embed=is_embed, is_display=is_display) # top-frame replay but through a proxy, redirect to original if is_top_frame and 'wsgiprox.proxy_host' in request.environ: kwargs['url'] = wb_url_obj.url kwargs['request_ts'] = wb_url_obj.timestamp self.browser_mgr.update_local_browser(kwargs) return redirect(wb_url_obj.url) try: self.check_if_content(wb_url_obj, request.environ, is_top_frame) request.environ['pywb.static_prefix'] = self.BUNDLE_PREFIX resp = self.render_content(wb_url, kwargs, request.environ) if frontend_cache_header: resp.status_headers.headers.append(frontend_cache_header) resp = HTTPResponse(body=resp.body, status=resp.status_headers.statusline, headers=resp.status_headers.headers) return resp except UpstreamException as ue: err_context = { 'url': ue.url, 'status': ue.status_code, 'error': ue.msg.get('error'), 'timestamp': wb_url_obj.timestamp if wb_url_obj else '', 'user': user, 'coll': coll_name, 'rec': rec_name, 'type': type, 'app_host': self.app_host, } @self.jinja2_view('content_error.html') def handle_error(error): response.status = ue.status_code return error if self.content_error_redirect: return redirect(self.content_error_redirect + '?' + urlencode(err_context), code=307) else: return handle_error(err_context)
def circuit_to_str(): try: circuits = json.loads(request.GET.get('circuit', '')) return json.dumps([deparse(circ) for circ in circuits]) except Exception as e: return HTTPResponse(status=500, body=str(e))
def monitoring_cp_get(topology_type=""): logger.info('GET monitoring-data({0}).'.format(const.TYPE_MON_CP)) # database handle. db_handle = DBHandle() try: # open topology database connection. tpldb_setup() # create <monitoring-data> xml_mon_data = Element(const.XML_TAG_MON_DATA) # topology_type==[physical | slice] logger.debug('topology type=' + topology_type) # param_type_list==[status | cpu_load | etc...] param_type_list = request.query.getall(const.HTTP_GET_OPT_TYPE) if param_type_list: logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_TYPE + ')=' + str(param_type_list)) # get HTTP GET option. param_topol = request.query.get(const.HTTP_GET_OPT_TOPOL) if param_topol: logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_TOPOL + ')=' + param_topol) param_node = request.query.get(const.HTTP_GET_OPT_NODE) if param_node: logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_NODE + ')=' + param_node) param_stime = request.query.get(const.HTTP_GET_OPT_STIME) if param_stime: logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_STIME + ')=' + param_stime) param_etime = request.query.get(const.HTTP_GET_OPT_ETIME) if param_etime: logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_ETIME + ')=' + param_etime) param_limit = request.query.get(const.HTTP_GET_OPT_LMT) if param_limit: logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_LMT + ')=' + param_limit) else: # set the default value if not specified. param_limit = str(const.DEFAULT_LIMIT) logger.debug('HTTP GET option (' + const.HTTP_GET_OPT_LMT + ')=default value(' + param_limit + ')') # required check.(topology_type) if not topology_type == const.TYPE_NW_PHYSICAL and \ not topology_type == const.TYPE_NW_SLICE: logger.warn('topology type is invalid. (' + topology_type + ')') return HTTPResponse( "GET monitoring-data({0}) error.(topology type({1}) is invalid.)" .format(const.TYPE_MON_CP, topology_type), status=400) # required check.(HTTP GET option) if not param_type_list: logger.warn( 'Required HTTP GET option({0}) have not been set.'.format( const.HTTP_GET_OPT_TYPE)) return HTTPResponse( "GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP, const.HTTP_GET_OPT_TYPE), status=400) if not param_stime: logger.warn( 'Required HTTP GET option({0}) have not been set.'.format( const.HTTP_GET_OPT_STIME)) return HTTPResponse( "GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP, const.HTTP_GET_OPT_STIME), status=400) if not param_etime: logger.warn( 'Required HTTP GET option({0}) have not been set.'.format( const.HTTP_GET_OPT_ETIME)) return HTTPResponse( "GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP, const.HTTP_GET_OPT_ETIME), status=400) # required check.(HTTP GET option:topology) nw_list = [] if not param_topol: # If the topology type is 'slice', topology(sliceID) is required if topology_type == const.TYPE_NW_SLICE: logger.warn( 'Required HTTP GET option({0}) have not been set.'.format( const.HTTP_GET_OPT_TOPOL)) return HTTPResponse( "GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP, const.HTTP_GET_OPT_TOPOL), status=400) # If the topology type is 'physical',target all network. elif topology_type == const.TYPE_NW_PHYSICAL: nw_list = get_all_network(const.TYPE_NW_PHYSICAL) # Without network specified, if the node is specified, the value of the node are ignored. param_node = None else: tmp_nw = Network.query.filter( Network.network_name == param_topol).first() if tmp_nw: nw_list.append(tmp_nw) # search target table name. tbl_name_list = search_target_table(logger, int(param_stime), int(param_etime), config.mon_data_cp_db) # add <topology_list> xml_topol_list = SubElement(xml_mon_data, const.XML_TAG_TOPOL_LIST) # connect to the database. db_con = db_handle.connect(config.mon_data_cp_db, config.db_addr, config.db_port, config.db_user, config.db_pass) for nw in nw_list: # add <topology type=topology_type name='xxx'> xml_topol = SubElement(xml_topol_list, const.XML_TAG_TOPOL, { const.XML_ATTR_TYPE: nw.type, const.XML_ATTR_NAME: nw.network_name }) node_list = [] if not param_node: # not specified. cover all of the node. if topology_type == const.TYPE_NW_SLICE: node_list = get_all_vm(nw) # If the topology type is 'physical',target all network. elif topology_type == const.TYPE_NW_PHYSICAL: node_list = get_all_srv(nw) else: if topology_type == const.TYPE_NW_SLICE: tmp_node = Node.query.filter(Node.node_name == param_node)\ .filter(Node.network_name == nw.network_name)\ .filter(Node.type == const.TYPE_NODE_VM).first() # If the topology type is 'physical',target all network. elif topology_type == const.TYPE_NW_PHYSICAL: tmp_node = Node.query.filter(Node.node_name == param_node)\ .filter(Node.network_name == nw.network_name)\ .filter(Node.type == const.TYPE_NODE_SRV).first() if tmp_node: node_list.append(tmp_node) for node in node_list: # add <node id='xxx' type='switch'> xml_node = SubElement( xml_topol, const.XML_TAG_NODE, { const.XML_ATTR_ID: node.node_name, const.XML_ATTR_TYPE: node.type }) for param_type in param_type_list: # add <parameter type=param_type> xml_param = SubElement(xml_node, const.XML_TAG_PARAM, {const.XML_ATTR_TYPE: param_type}) # no data in the search time-range. if not tbl_name_list: continue # search database.(metaID) sql = "(SELECT metaID FROM metaData " \ + "WHERE network_name='{0}' AND node_name='{1}' AND type='{2}')"\ .format(nw.network_name,node.node_name,param_type) logger.debug(sql) db_con.execute(sql) res_metaid = db_con.fetchone() if res_metaid: metaid = res_metaid['metaID'] else: continue # search database.(data) sql_base = "(SELECT timestamp,value FROM {0} WHERE metaID={1}"\ + " AND timestamp >= {0}".format(param_stime)\ + " AND timestamp <= {0})".format(param_etime) sql_list = [] for tbl_name in tbl_name_list: sql_list.append(sql_base.format(tbl_name, metaid)) sql = " UNION ".join(sql_list) sql += " ORDER BY timestamp DESC LIMIT " + param_limit logger.debug(sql) db_con.execute(sql) res_mon_data = db_con.fetchall() for mon_data in res_mon_data: # add <value timestamp=mon_data["timestamp"]> xml_value = SubElement(xml_param, const.XML_TAG_VAL, { const.XML_ATTR_TIME_STAMP: str(mon_data["timestamp"]) }) # add <value>mon_data["value"]</value> tmp_value = util.isinteger(float(mon_data["value"])) xml_value.text = str(tmp_value) except Exception: logger.exception('GET monitoring-data({0}) error.'.format( const.TYPE_MON_CP)) return HTTPResponse("GET monitoring-data({0}) error.".format( const.TYPE_MON_CP), status=500) finally: # close monitoring database connection. db_handle.close() # close topology database connection. tpldb_close() return util.prettify(xml_mon_data)
def delete(self, list_id): self.dao.delete_list(list_id) return HTTPResponse(status=204)
def set_json_body(body): r = HTTPResponse(status=200, body=body) r.set_header('Content-Type', 'application/json') return r
def get_routes(lang, city, limit=3): station_from = int( request.query.station_from) if request.query.station_from else None station_to = int( request.query.station_to) if request.query.station_to else None portal_from = int( request.query.portal_from) if request.query.portal_from else None portal_to = int( request.query.portal_to) if request.query.portal_to else None # Извлечение информации о станции def get_station_info(station_id): for station in STATIONS[city]: if station['id_station'] == str(station_id): return dict(name=station.get('name_' + lang, station.get('name_en')), line=int(station['id_line']), coords=(float(station['lat']), float(station['lon'])), node_id=station['id_node']) # Извлечение информации о линии def get_line_info(line_id): for line in LINES[city]: if line['id_line'] == str(line_id): return dict(name=line.get('name_' + lang, line.get('name_en')), color=line['color']) # Проверка на принадлежность станций к одной линии def check_the_same_line(node1, node2): node1_line = get_station_info(node1)['line'] node2_line = get_station_info(node2)['line'] return node1_line == node2_line # Заполнение информации о препятствиях на входах и выходах def portal_barriers(portal_id): for portal in PORTALS[city]: if int(portal['id_entrance']) == portal_id: return get_barriers(portal) # Заполнение информации о препятствиях на переходах def interchange_barriers(station_from, station_to): for interchange in INTERCHANGES[city]: if (int(interchange['station_from']) == station_from) and (int( interchange['station_to']) == station_to): return get_barriers(interchange) if ((station_from is not None) and (station_to is not None)): all_shortest_paths = nx.all_shortest_paths(GRAPH[city], station_from, station_to, weight='weight') routes = [] for index, path in enumerate(all_shortest_paths): if index == limit: break route = [] for station in path: station_info = get_station_info(station) line_id = station_info['line'] same_line = check_the_same_line(station, get_next_item(path, station)) station_type = "regular" if same_line else "interchange" node_id = station_info['node_id'] unit = dict(id=station, station_type=station_type, station_name=station_info['name'], coordinates=station_info['coords'], station_line=dict( id=line_id, name=get_line_info(line_id)['name'], color=get_line_info(line_id)['color']), schema=SCHEMAS[city][str(node_id)] if str(node_id) in SCHEMAS[city] else None) if station_type == "interchange": unit['barriers'] = None unit['barriers'] = interchange_barriers( station, get_next_item(path, station)) elif station_type == "regular": unit['barriers'] = None route.append(unit) # Заполняем информацию о входах if portal_from is not None: portal_from_obj = filter( lambda portal: portal['id_entrance'] == str(portal_from), PORTALS[city])[0] if portal_to is not None: portal_to_obj = filter( lambda portal: portal['id_entrance'] == str(portal_to), PORTALS[city])[0] portals = dict( portal_from=dict( barriers=portal_barriers(portal_from), meetcode='#%s' % portal_from_obj['meetcode'], name='%s' % portal_from_obj.get('name_' + lang, portal_from_obj.get('name_en'))) if portal_from else None, portal_to=dict(barriers=portal_barriers(portal_to), meetcode='#%s' % portal_to_obj['meetcode'], name='%s' % portal_to_obj.get('name_' + lang, portal_to_obj.get('name_en'))) if portal_to else None) routes.append(dict(route=route, portals=portals)) response.content_type = 'application/json' return dumps(dict(result=routes)) else: return HTTPResponse(status=400)
def list(): return HTTPResponse(status=200, body=json.dumps(teachers))
def list_notes(request): notes = Note.select().where(Note.user == request.user.id) schema = NoteSchema(many=True) result = schema.dump(list(notes)) return HTTPResponse({'results': result.data}, status=200)
def get(id): id = int(id) return HTTPResponse(status=200, body=json.dumps(teachers[id]))
def search(): id = int(request.query.id) return HTTPResponse(status=200, body=json.dumps(teachers[id]))
def success_get(cls, result_object): return HTTPResponse(result_object, 200, HEADERS_RESPONSE)
def error_handler(response): """Simple error handler that doesn't use the Bottle error template.""" if request.is_ajax: return HTTPResponse(body=response.body, status=response.status) else: return request.app.default_error_handler(response)