Python flask.make_response() Examples
The following are 30 code examples for showing how to use flask.make_response(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
flask
, or try the search function
.
Example 1
Project: grlc Author: CLARIAH File: server.py License: MIT License | 8 votes |
def query(user, repo, query_name, subdir=None, spec_url=None, sha=None, content=None): """Execute SPARQL query for a specific grlc-generated API endpoint""" glogger.info("-----> Executing call name at /{}/{}/{}/{} on commit {}".format(user, repo, subdir, query_name, sha)) glogger.debug("Request accept header: " + request.headers["Accept"]) requestArgs = request.args acceptHeader = request.headers['Accept'] requestUrl = request.url formData = request.form query_response, status, headers = utils.dispatch_query(user, repo, query_name, subdir, spec_url, sha=sha, content=content, requestArgs=requestArgs, acceptHeader=acceptHeader, requestUrl=requestUrl, formData=formData) if isinstance(query_response, list): query_response = jsonify(query_response) return make_response(query_response, status, headers) ### Server routes ###
Example 2
Project: oabot Author: dissemin File: app.py License: MIT License | 6 votes |
def stream_url(): url = flask.request.args.get('url') r = requests.get(url) # If it's just an HTML page served over HTTPS, no problem if url.startswith('https://') and ( 'text/html' in r.headers['Content-Type'] ): return flask.redirect(flask.url_for('redirect_to_url', url=url)) response = flask.make_response() response.data = r.content response.headers['Content-Type'] = r.headers['Content-Type'] # Preserve filename if possible if 'Content-Disposition' in r.headers: response.headers['Content-Disposition'] = r.headers['Content-Disposition'].replace("attachment;", "inline;") # Work around incorrect application/octet-stream if 'zenodo.org' in url: response.headers['Content-Type'] = 'application/pdf' return response
Example 3
Project: beavy Author: beavyHQ File: __init__.py License: Mozilla Public License 2.0 | 6 votes |
def api_only(fn): @wraps(fn) def wrapped(*args, **kwargs): accepted = set(request.accept_mimetypes.values()) explicit = not(not request.args.get("json", False)) if not (accepted & API_MIMETYPES) and not explicit: return abort(415, "Unsupported Media Type") resp = fn(*args, **kwargs) if not isinstance(resp, ResponseBase): data, code, headers = unpack(resp) # we've found one, return json if isinstance(data, MarshalResult): data = data.data resp = make_response(json.dumps(data, indent=explicit and 4 or 0), code) if headers: resp.headers.update(headers) resp.headers["Content-Type"] = 'application/json' return resp return wrapped
Example 4
Project: api-pycon2014 Author: miguelgrinberg File: decorators.py License: MIT License | 6 votes |
def rate_limit(limit, per, scope_func=lambda: request.remote_addr): def decorator(f): @functools.wraps(f) def wrapped(*args, **kwargs): if current_app.config['USE_RATE_LIMITS']: key = 'rate-limit/%s/%s/' % (f.__name__, scope_func()) limiter = RateLimit(key, limit, per) if not limiter.over_limit: rv = f(*args, **kwargs) else: rv = too_many_requests('You have exceeded your request rate') #rv = make_response(rv) g.headers = { 'X-RateLimit-Remaining': str(limiter.remaining), 'X-RateLimit-Limit': str(limiter.limit), 'X-RateLimit-Reset': str(limiter.reset) } return rv else: return f(*args, **kwargs) return wrapped return decorator
Example 5
Project: api-pycon2014 Author: miguelgrinberg File: decorators.py License: MIT License | 6 votes |
def etag(f): @functools.wraps(f) def wrapped(*args, **kwargs): # only for HEAD and GET requests assert request.method in ['HEAD', 'GET'],\ '@etag is only supported for GET requests' rv = f(*args, **kwargs) rv = make_response(rv) etag = '"' + hashlib.md5(rv.get_data()).hexdigest() + '"' rv.headers['ETag'] = etag if_match = request.headers.get('If-Match') if_none_match = request.headers.get('If-None-Match') if if_match: etag_list = [tag.strip() for tag in if_match.split(',')] if etag not in etag_list and '*' not in etag_list: rv = precondition_failed() elif if_none_match: etag_list = [tag.strip() for tag in if_none_match.split(',')] if etag in etag_list or '*' in etag_list: rv = not_modified() return rv return wrapped
Example 6
Project: github-stats Author: lipis File: welcome.py License: MIT License | 6 votes |
def person(): limit = min(int(util.param('limit', int) or flask.request.cookies.get('limit') or config.MAX_DB_LIMIT), config.MAX_DB_LIMIT) order = util.param('order') or '-stars' if 'repo' in order: order = '-public_repos' elif 'follower' in order: order = '-followers' person_dbs, person_cursor = model.Account.get_dbs( order=order, organization=False, limit=limit, ) response = flask.make_response(flask.render_template( 'account/list_person.html', title='People', description='Top People on GitHub', html_class='account-person', person_dbs=person_dbs, order=order, limit=limit, )) response.set_cookie('limit', str(limit)) return response
Example 7
Project: github-stats Author: lipis File: welcome.py License: MIT License | 6 votes |
def organization(): limit = min(int(util.param('limit', int) or flask.request.cookies.get('limit') or config.MAX_DB_LIMIT), config.MAX_DB_LIMIT) order = util.param('order') or '-stars' if 'repo' in order: order = '-public_repos' organization_dbs, organization_cursor = model.Account.get_dbs( order=order, organization=True, limit=limit, ) response = flask.make_response(flask.render_template( 'account/list_organization.html', title='Organizations', description='Top Organizations on GitHub', html_class='account-organization', organization_dbs=organization_dbs, order=order, limit=limit, )) response.set_cookie('limit', str(limit)) return response
Example 8
Project: calibre-web Author: janeczku File: helper.py License: GNU General Public License v3.0 | 6 votes |
def do_download_file(book, book_format, client, data, headers): if config.config_use_google_drive: startTime = time.time() df = gd.getFileFromEbooksFolder(book.path, data.name + "." + book_format) log.debug('%s', time.time() - startTime) if df: return gd.do_gdrive_download(df, headers) else: abort(404) else: filename = os.path.join(config.config_calibre_dir, book.path) if not os.path.isfile(os.path.join(filename, data.name + "." + book_format)): # ToDo: improve error handling log.error('File not found: %s', os.path.join(filename, data.name + "." + book_format)) if client == "kobo" and book_format == "kepub": headers["Content-Disposition"] = headers["Content-Disposition"].replace(".kepub", ".kepub.epub") response = make_response(send_from_directory(filename, data.name + "." + book_format)) # ToDo Check headers parameter for element in headers: response.headers[element[0]] = element[1] return response ##################################
Example 9
Project: python-slackclient Author: slackapi File: views_default_to_current_conversation.py License: MIT License | 6 votes |
def slack_app(): if not signature_verifier.is_valid_request(request.get_data(), request.headers): return make_response("invalid request", 403) if "command" in request.form \ and request.form["command"] == "/view": trigger_id = request.form["trigger_id"] return open_modal(trigger_id) elif "payload" in request.form: payload = json.loads(request.form["payload"]) if payload["type"] == "view_submission" \ and payload["view"]["callback_id"] == "modal-id": submitted_data = payload["view"]["state"]["values"] print(submitted_data) # {'b-id': {'a-id': {'type': 'plain_text_input', 'value': 'your input'}}} return make_response("", 200) if payload["type"] == "shortcut" \ and payload["callback_id"] == "view-test": return open_modal(payload["trigger_id"]) return make_response("", 404)
Example 10
Project: python-slackclient Author: slackapi File: issue_497.py License: MIT License | 6 votes |
def per_request(): try: client = WebClient( token=os.environ["SLACK_BOT_TOKEN"], run_async=False ) response = client.chat_postMessage( channel="#random", text="You used a new WebClient for posting this message!" ) return str(response) except SlackApiError as e: return make_response(str(e), 400) # This doesn't work
Example 11
Project: python-slackclient Author: slackapi File: issue_497.py License: MIT License | 6 votes |
def per_request_async(): try: # This is not optimal and the host should have a large number of FD (File Descriptor) loop_for_this_request = asyncio.new_event_loop() async_client = WebClient( token=os.environ["SLACK_BOT_TOKEN"], run_async=True, loop=loop_for_this_request ) future = async_client.chat_postMessage( channel="#random", text="You used the singleton WebClient for posting this message!" ) response = loop_for_this_request.run_until_complete(future) return str(response) except SlackApiError as e: return make_response(str(e), 400)
Example 12
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def job_create(): try: job = bass.create_job() return jsonify(message = "ok", job = job.json()) except Exception as ex: return make_response(jsonify(message = str(ex), trace = traceback.format_exc()), 400)
Example 13
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def job_get_status(job_id): try: return jsonify(message = "ok", job = bass.get_job(job_id).json()) except KeyError: return make_response(jsonify(message = "Invalid job id"), 400) except Exception as ex: return make_response(jsonify(message = str(ex), trace = traceback.format_exc()), 400)
Example 14
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def job_add_sample(job_id): try: samples = [] for name, file_ in request.files.items(): handle, filename = tempfile.mkstemp() os.close(handle) file_.save(filename) samples.append(bass.get_job(job_id).add_sample(filename, name)) return jsonify(message = "ok", samples = [s.json() for s in samples]) except KeyError: log.exception("Invalid job id") return make_response(jsonify(message = "Invalid job id"), 400)
Example 15
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def job_submit(job_id): try: bass.submit_job(job_id) return jsonify(message = "ok") except KeyError: return make_response(jsonify(message = "Invalid job id"), 400)
Example 16
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def job_delete(job_id): try: bass.delete_job(job_id) return jsonify(message = "ok") except KeyError: return make_response(jsonify(message = "Invalid job id"), 400)
Example 17
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def function_get(fid): global Session session = Session() try: function = session.query(Function).filter(Function.id == fid).one() return make_response(jsonify(**json.loads(function.data)), 200) except NoResultFound: return make_response(jsonify(message = "Function not found"), 404)
Example 18
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def function_raw_hash_get(): global Session session = Session() filename, file_ = request.files.items()[0] db = Database(pickle.load(file_)) arch_name = db.architecture_name if arch_name == "metapc": arch_name = "x86" try: arch = session.query(Architecture).filter(Architecture.name == arch_name and \ Architecture.bits == db.architecture_bits and \ Architecture.little_endian == db.architecture_endianness == "little").one() except NoResultFound: return make_response(jsonify(message = "Architecture not found"), 404) try: func = next(db.functions) except StopIteration: return make_response(jsonify(message = "No function found in database"), 500) raw_hash = _function_calculate_raw_sha256(func) size = _function_get_size(func) try: function = session.query(Function).filter(Function.raw_sha256 == raw_hash and \ Function.size == size and \ Function.arch == arch.id).one() return make_response(jsonify(**json.loads(function.data)), 200) except NoResultFound: return make_response(jsonify(message = "Function not found"), 404)
Example 19
Project: BASS Author: Cisco-Talos File: server.py License: GNU General Public License v2.0 | 5 votes |
def function_mnem_hash_get(): global Session session = Session() filename, file_ = request.files.items()[0] db = Database(pickle.load(file_)) arch_name = db.architecture_name if arch_name == "metapc": arch_name = "x86" try: arch = session.query(Architecture).filter(Architecture.name == arch_name and \ Architecture.bits == db.architecture_bits and \ Architecture.little_endian == db.architecture_endianness == "little").one() except NoResultFound: return make_response(jsonify(message = "Architecture not found"), 404) try: func = next(db.functions) except StopIteration: return make_response(jsonify(message = "No function found in database"), 500) mnem_hash = _function_calculate_mnem_sha256(func) try: function = session.query(Function).filter(Function.mnem_sha256 == mnem_hash and \ Function.arch == arch.id).one() return make_response(jsonify(**json.loads(function.data)), 200) except NoResultFound: return make_response(jsonify(message = "Function not found"), 404)
Example 20
Project: BASS Author: Cisco-Talos File: ida_service.py License: GNU General Public License v2.0 | 5 votes |
def bindiff_export(): """ Run the IDA Pro autoanalysis on the input file and export a BinExport database. :param input: The input file :return: Status code 200 and a JSON object containing the output database name in key 'output', or status code 422 on invalid parameters, 408 on timeout or 500 on other errors. """ logger.info("bindiff_export called") directory = None try: directory = tempfile.mkdtemp() if len(request.files) != 1: return make_response(jsonify(error = "Missing file parameter"), 422) filename, file_ = request.files.items()[0] input_ = os.path.join(directory, sanitize_filename(filename)) file_.save(input_) output = os.path.join(directory, "output.BinExport") timeout = request.form.get('timeout', None) is_64_bit = request.form.get('is_64_bit', True) try: run_ida(input_, is_64_bit, timeout, os.path.join(PREFIX, "export_binexport_pickle.py"), "binexport", output) logger.info("Command completed successfully") return send_file(open(output, "rb"), as_attachment = True, attachment_filename = "%s.BinExport" % filename, mimetype = "application/binary") except TimeoutError: return jsonify(error = "Program execution timed out"), 408 except OSError as err: return jsonify(error = "Program execution failed with error %d" % err.errno), 500 finally: if directory is not None: shutil.rmtree(directory)
Example 21
Project: BASS Author: Cisco-Talos File: ida_service.py License: GNU General Public License v2.0 | 5 votes |
def pickle_export(): """ Run the IDA Pro autoanalysis on the input file and export a BinExport database. :param input: The input file :return: Status code 200 and a JSON object containing the output database name in key 'output', or status code 422 on invalid parameters, 408 on timeout or 500 on other errors. """ logger.info("bindiff_export called") directory = None try: directory = tempfile.mkdtemp() if len(request.files) != 1: return make_response(jsonify(error = "Missing file parameter"), 422) filename, file_ = request.files.items()[0] input_ = os.path.join(directory, sanitize_filename(filename)) file_.save(input_) output = os.path.join(directory, "output.pickle") timeout = request.form.get('timeout', None) is_64_bit = request.form.get('is_64_bit', False) try: run_ida(input_, is_64_bit, timeout, os.path.join(PREFIX, "export_binexport_pickle.py"), "pickle", output) logger.info("Command completed successfully") return send_file(open(output, "rb"), as_attachment = True, attachment_filename = "%s.pickle" % filename, mimetype = "application/binary") except TimeoutError: return jsonify(error = "Program execution timed out"), 408 except OSError as err: return jsonify(error = "Program execution failed with error %d" % err.errno), 500 finally: if directory is not None: shutil.rmtree(directory)
Example 22
Project: BASS Author: Cisco-Talos File: ida_service.py License: GNU General Public License v2.0 | 5 votes |
def bindiff_compare(): logger.info("bindiff_compare called") input_dir = tempfile.mkdtemp() output_dir = tempfile.mkdtemp() try: primary = os.path.join(input_dir, "primary") secondary = os.path.join(input_dir, "secondary") try: request.files["primary"].save(primary) request.files["secondary"].save(secondary) except KeyError: return make_response(jsonify(error="Missing parameter 'primary' or 'secondary'"), 422) timeout = request.form.get('timeout', None) cmd = (BINDIFF_DIFFER, "--primary", primary, "--secondary", secondary, "--output_dir", output_dir) logger.info("Executing %s", " ".join("'%s'" % x for x in cmd)) check_call(cmd, cwd = output_dir, timeout = timeout) db_path = [os.path.join(output_dir, x) for x in os.listdir(output_dir)] if len(db_path) != 1: return make_response(jsonify(error = "BinDiff generated 0 or several output files"), 500) return send_file(open(db_path[0], "rb"), as_attachment = True, attachment_filename = "BinDiff.sqlite3", mimetype = "application/binary") except OSError as err: if err.errno == -9: return make_response(jsonify(error = "Program execution timed out"), 408) else: return make_response(jsonify(error = "Program execution failed with error %d" % err.errno), 500) finally: shutil.rmtree(input_dir) shutil.rmtree(output_dir)
Example 23
Project: BASS Author: Cisco-Talos File: ida_service.py License: GNU General Public License v2.0 | 5 votes |
def bindiff_export(): """ Run the IDA Pro autoanalysis on the input file and export a BinExport database. :param input: The input file :return: Status code 200 and a JSON object containing the output database name in key 'output', or status code 422 on invalid parameters, 408 on timeout or 500 on other errors. """ logger.info("bindiff_export called") directory = None try: directory = tempfile.mkdtemp() if len(request.files) != 1: return make_response(jsonify(error = "Missing file parameter"), 422) filename, file_ = request.files.items()[0] input_ = os.path.join(directory, sanitize_filename(filename)) file_.save(input_) output = os.path.join(directory, "output.BinExport") timeout = request.form.get('timeout', None) is_64_bit = request.form.get('is_64_bit', True) try: run_ida(input_, is_64_bit, timeout, os.path.join(PREFIX, "export_binexport_pickle.py"), "binexport", output) logger.info("Command completed successfully") return send_file(open(output, "rb"), as_attachment = True, attachment_filename = "%s.BinExport" % filename, mimetype = "application/binary") except TimeoutError: return jsonify(error = "Program execution timed out"), 408 except OSError as err: return jsonify(error = "Program execution failed with error %d" % err.errno), 500 finally: if directory is not None: shutil.rmtree(directory)
Example 24
Project: BASS Author: Cisco-Talos File: ida_service.py License: GNU General Public License v2.0 | 5 votes |
def bindiff_pickle_export(): """ Run the IDA Pro autoanalysis on the input file and export a BinExport database. :param input: The input file :return: Status code 200 and a JSON object containing the output database name in key 'output', or status code 422 on invalid parameters, 408 on timeout or 500 on other errors. """ logger.info("bindiff_pickle_export called") directory = None try: directory = tempfile.mkdtemp() if len(request.files) != 1: return make_response(jsonify(error = "Missing file parameter"), 422) filename, file_ = request.files.items()[0] input_ = os.path.join(directory, sanitize_filename(filename)) file_.save(input_) output_binexport = os.path.join(directory, "output.BinExport") output_pickle = os.path.join(directory, "output.pickle") timeout = request.form.get('timeout', None) is_64_bit = request.form.get('is_64_bit', True) try: run_ida(input_, is_64_bit, timeout, os.path.join(PREFIX, "export_binexport_pickle.py"), "binexport_pickle", output_binexport, output_pickle) logger.info("Command completed successfully") output_tar = os.path.join(directory, "output.tar.gz") subprocess.check_call(["tar", "czf", output_tar, os.path.relpath(output_binexport, directory), os.path.relpath(output_pickle, directory)], cwd = directory) return send_file(open(output_tar, "rb"), as_attachment = True, attachment_filename = "%s.tar.gz" % filename, mimetype = "application/gzip") except TimeoutError: return jsonify(error = "Program execution timed out"), 408 except OSError as err: return jsonify(error = "Program execution failed with error %d" % err.errno), 500 finally: if directory is not None: shutil.rmtree(directory)
Example 25
Project: BASS Author: Cisco-Talos File: ida_service.py License: GNU General Public License v2.0 | 5 votes |
def bindiff_compare(): logger.info("bindiff_compare called") input_dir = tempfile.mkdtemp() output_dir = tempfile.mkdtemp() try: primary = os.path.join(input_dir, "primary") secondary = os.path.join(input_dir, "secondary") try: request.files["primary"].save(primary) request.files["secondary"].save(secondary) except KeyError: return make_response(jsonify(error="Missing parameter 'primary' or 'secondary'"), 422) timeout = request.form.get('timeout', None) cmd = (BINDIFF_DIFFER, "--primary", primary, "--secondary", secondary, "--output_dir", output_dir) logger.info("Executing %s", " ".join("'%s'" % x for x in cmd)) check_call(cmd, cwd = output_dir, timeout = timeout) db_path = [os.path.join(output_dir, x) for x in os.listdir(output_dir)] if len(db_path) != 1: return make_response(jsonify(error = "BinDiff generated 0 or several output files"), 500) return send_file(open(db_path[0], "rb"), as_attachment = True, attachment_filename = "BinDiff.sqlite3", mimetype = "application/binary") except OSError as err: if err.errno == -9: return make_response(jsonify(error = "Program execution timed out"), 408) else: return make_response(jsonify(error = "Program execution failed with error %d" % err.errno), 500) finally: shutil.rmtree(input_dir) shutil.rmtree(output_dir)
Example 26
Project: zmirror Author: aploium File: utils.py License: MIT License | 5 votes |
def generate_simple_resp_page(errormsg=b'We Got An Unknown Error', error_code=500): """ :type errormsg: bytes :type error_code: int :rtype: Response """ return make_response(errormsg, error_code)
Example 27
Project: grlc Author: CLARIAH File: server.py License: MIT License | 5 votes |
def swagger_spec(user, repo, subdir=None, spec_url=None, sha=None, content=None): """ Generate swagger specification """ glogger.info("-----> Generating swagger spec for /{}/{}, subdir {}, params {}, on commit {}".format(user, repo, subdir, spec_url, sha)) swag = utils.build_swagger_spec(user, repo, subdir, spec_url, sha, static.SERVER_NAME) resp_spec = make_response(jsonify(swag)) resp_spec.headers['Content-Type'] = 'application/json' resp_spec.headers['Cache-Control'] = static.CACHE_CONTROL_POLICY # Caching JSON specs for 15 minutes glogger.info("-----> API spec generation for /{}/{}, subdir {}, params {}, on commit {} complete".format(user, repo, subdir, spec_url, sha)) return resp_spec
Example 28
Project: grlc Author: CLARIAH File: server.py License: MIT License | 5 votes |
def grlc(): """Grlc landing page.""" resp = make_response(render_template('index.html')) return resp ############################# ### Routes for local APIs ### ############################# # Spec generation, front-end
Example 29
Project: landmarkerio-server Author: menpo File: server.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def safe_send_file(mimetype, path, fail_message, gzip=False): try: r = make_response(send_file(path, mimetype=mimetype)) if gzip: r.headers['Content-Encoding'] = 'gzip' return r except Exception as e: print(e) return abort(404, message=fail_message)
Example 30
Project: hackernewsbot Author: phil-r File: main.py License: MIT License | 5 votes |
def story_redirect(short_id): """Redirect to story url""" try: story_id = str(shortener.decode(short_id)) except: return abort(400) redirect_url = memcache.get(story_id) if not redirect_url: story = ndb.Key(StoryPost, story_id).get() if not story: return make_response('<h1>Service Unavailable</h1><p>Try again later</p>', 503, {'Retry-After': 5}) story.add_memcache() redirect_url = story.url return redirect(redirect_url)