本文整理汇总了Python中sanic.response.file方法的典型用法代码示例。如果您正苦于以下问题:Python response.file方法的具体用法?Python response.file怎么用?Python response.file使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.response
的用法示例。
在下文中一共展示了response.file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_file
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def download_file(request, id):
try:
query = await db_model.filemeta_query()
file_meta = await db_objects.get(query, agent_file_id=id)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'file not found'})
# now that we have the file metadata, get the file if it's done downloading
if file_meta.complete and not file_meta.deleted:
try:
return await file(file_meta.path, filename=file_meta.path.split("/")[-1])
except Exception as e:
print(str(e))
print("File not found")
return json({"status": "error", "error": "File doesn't exist on disk"}, status=404)
elif not file_meta.complete:
print("File not done downloading")
return json({"status": "error", "error": "File not finished uploading to server"}, status=404)
else:
print("File was deleted")
return json({"status": "error", "error": "File deleted or not finished uploading to server"}, status=404)
# this is the function for the 'upload' action of file from Apfell to agent
示例2: remove_uploaded_files_for_command
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def remove_uploaded_files_for_command(request, user, cid):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
try:
query = await db_model.command_query()
command = await db_objects.get(query, id=cid)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to get command'})
try:
data = request.json
path = os.path.abspath("./app/payloads/{}/commands/{}".format(command.payload_type.ptype, command.cmd))
if data['folder'] == "" or data['folder'] is None:
data['folder'] = "."
attempted_path = os.path.abspath(path + "/" + data['folder'] + "/" + data['file'])
if path in attempted_path:
os.remove(attempted_path)
if data['folder'] == ".":
data['folder'] = ""
return json({'status': 'success', 'folder': data['folder'], 'file': data['file']})
return json({'status': 'error', 'error': 'failed to find file'})
except Exception as e:
return json({'status': 'error', 'error': 'failed getting files: ' + str(e)})
示例3: download_files_for_command
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def download_files_for_command(request, user, cid):
try:
query = await db_model.command_query()
command = await db_objects.get(query, id=cid)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to get command'})
try:
data = request.json
if 'file' not in data:
return json({'status': 'error', 'error': 'failed to get file parameter'})
if 'folder' not in data:
data['folder'] = "."
path = os.path.abspath("./app/payloads/{}/commands/{}".format(command.payload_type.ptype, command.cmd))
if data['folder'] == "" or data['folder'] is None:
data['folder'] = "."
attempted_path = os.path.abspath(path + "/" + data['folder'] + "/" + data['file'])
if path in attempted_path:
code = open(attempted_path, 'rb')
encoded = base64.b64encode(code.read()).decode("UTF-8")
return json({"status": "success", "file": encoded})
return json({'status': 'error', 'error': 'failed to read file'})
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
示例4: remove_file_for_c2profiles
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def remove_file_for_c2profiles(request, info, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
name = unquote_plus(info)
try:
query = await db_model.c2profile_query()
profile = await db_objects.get(query, name=name)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to find C2 Profile'})
try:
data = request.json
path = os.path.abspath("./app/c2_profiles/{}/".format(profile.name))
if data['folder'] != "":
attempted_path = os.path.abspath(path + "/" + data['folder'] + "/" + data['file'])
else:
attempted_path = os.path.abspath(path + "/" + data['file'])
if path in attempted_path and attempted_path != path:
os.remove(attempted_path)
return json({'status': 'success', 'folder': data['folder'], 'file': data['file']})
return json({'status': 'error', 'error': 'failed to find file'})
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
示例5: remove_folder_for_c2profiles
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def remove_folder_for_c2profiles(request, info, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
name = unquote_plus(info)
try:
query = await db_model.c2profile_query()
profile = await db_objects.get(query, name=name)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to find C2 Profile'})
try:
data = request.json
path = os.path.abspath("./app/c2_profiles/{}/".format(profile.name))
if data['folder'] != "":
attempted_path = os.path.abspath(path + "/" + data['folder'])
else:
return json({'status': "error", "error": "Must supply folder path"})
if path in attempted_path and attempted_path != path:
os.rmdir(attempted_path)
return json({'status': 'success', 'folder': data['folder']})
return json({'status': 'error', 'error': 'path must be in the right area'})
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
示例6: remove_container_file_for_c2profiles
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def remove_container_file_for_c2profiles(request, info, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
name = unquote_plus(info)
try:
query = await db_model.c2profile_query()
profile = await db_objects.get(query, name=name)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to find C2 Profile'})
try:
data = request.json
status = await send_c2_rabbitmq_message(profile.name, "removefile", js.dumps(data), user['username'])
return json(status)
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
示例7: remove_container_folder_for_c2profiles
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def remove_container_folder_for_c2profiles(request, info, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
name = unquote_plus(info)
try:
query = await db_model.c2profile_query()
profile = await db_objects.get(query, name=name)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to find C2 Profile'})
try:
data = request.json
status = await send_c2_rabbitmq_message(profile.name, "removefolder", js.dumps(data), user['username'])
return json(status)
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
示例8: add_container_folder_for_c2profiles
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def add_container_folder_for_c2profiles(request, info, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
name = unquote_plus(info)
try:
query = await db_model.c2profile_query()
profile = await db_objects.get(query, name=name)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to find C2 Profile'})
try:
data = request.json
status = await send_c2_rabbitmq_message(profile.name, "addfolder", js.dumps(data), user['username'])
return json(status)
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
示例9: download_container_file_for_c2profiles
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def download_container_file_for_c2profiles(request, info, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
name = unquote_plus(info)
try:
query = await db_model.c2profile_query()
profile = await db_objects.get(query, name=name)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to find C2 Profile'})
try:
data = request.json
status = await send_c2_rabbitmq_message(profile.name, "getfile", js.dumps(data), user['username'])
return json(status)
except Exception as e:
return json({'status': 'error', 'error': 'failed finding the file: ' + str(e)})
# Delete a profile
示例10: import_c2_profile
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def import_c2_profile(request, user):
if user['auth'] not in ['access_token', 'apitoken']:
abort(status_code=403, message="Cannot access via Cookies. Use CLI or access via JS in browser")
if request.files:
try:
data = js.loads(request.files['upload_file'][0].body)
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to parse file'})
else:
try:
data = request.json
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to parse JSON'})
try:
query = await db_model.operator_query()
operator = await db_objects.get(query, username=user['username'])
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to get operator information'})
return json(await import_c2_profile_func(data, operator))
示例11: register_default_profile_operation
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def register_default_profile_operation(operator):
try:
file = open('./app/default_files/other_info/default_c2_db_info.json', 'r')
c2_data = js.load(file) # this is a lot of data and might take a hot second to load
for p in c2_data['profiles']:
print("Creating profile for: " + p['name'])
query = await db_model.payloadtypec2profile_query()
mappings = await db_objects.execute(query.switch(C2Profile).where(C2Profile.name == p['name']))
for m in mappings:
print("removing {}".format(m.payload_type.ptype))
await db_objects.delete(m)
await import_c2_profile_func(p, operator)
return {'status': 'success'}
except Exception as e:
print(e)
return {'status': 'error', 'error': str(e)}
示例12: create_payload_func
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def create_payload_func(data, user):
if 'tag' not in data:
data['tag'] = data['payload_type'] + " payload created by " + user['username']
# first we need to register the payload
rsp = await register_new_payload_func(data, user)
if rsp['status'] == "success":
# now that it's registered, write the file, if we fail out here then we need to delete the db object
query = await db_model.payload_query()
payload = await db_objects.get(query, uuid=rsp['uuid'])
if payload.payload_type.external is False:
create_rsp = await write_payload(payload.uuid, user, data)
if create_rsp['status'] == "success":
return {'status': 'success',
'uuid': rsp['uuid']}
else:
await db_objects.delete(payload, recursive=True)
return {'status': 'error', 'error': create_rsp['error']}
else:
payload.build_phase = "success"
payload.build_message = "Created externally, not hosted in Apfell"
await db_objects.update(payload)
return {'status': 'success', 'uuid': rsp['uuid']}
else:
print(rsp['error'])
return {'status': 'error', 'error': rsp['error']}
示例13: get_payload
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def get_payload(request, uuid, user):
# return a blob of the requested payload
# the pload string will be the uuid of a payload registered in the system
try:
query = await db_model.payload_query()
payload = await db_objects.get(query, uuid=uuid)
except Exception as e:
return json({'status': 'error', 'error': 'payload not found'})
if payload.operation.name in user['operations']:
try:
return await file(payload.location, filename=payload.location.split("/")[-1])
except Exception as e:
print(e)
return json({'status': 'error', 'error': 'failed to open payload'})
else:
return json({'status': 'error', 'error': 'you\'re not part of the right operation'})
示例14: _write_data_to_file
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def _write_data_to_file(conversation_id: Text, endpoint: EndpointConfig):
"""Write stories and nlu data to file."""
story_path, nlu_path, domain_path = _request_export_info()
tracker = await retrieve_tracker(endpoint, conversation_id)
events = tracker.get("events", [])
serialised_domain = await retrieve_domain(endpoint)
domain = Domain.from_dict(serialised_domain)
await _write_stories_to_file(story_path, events, domain)
await _write_nlu_to_file(nlu_path, events)
await _write_domain_to_file(domain_path, events, domain)
logger.info("Successfully wrote stories and NLU data")
示例15: spotify_callback
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import file [as 别名]
def spotify_callback(request):
if request.args.get("error"):
return json(dict(error=request.args.get("error_description")))
elif request.args.get("code"):
grant = request.args.get("code")
callback_state = request.args.get("state")
if callback_state != state:
abort(401)
try:
user_creds = await spt.build_user_creds(grant=grant)
async with aiofiles.open(os.getcwd() + "SPOTIFY_CREDS.json", "w") as file:
await file.write(stdlib_json.dumps(user_creds.__dict__))
except AuthError as e:
return json(dict(error_description=e.msg, error_code=e.code), e.code)
else:
await spt.populate_user_creds()
print(os.getcwd())
return await response.file(os.getcwd() + "SPOTIFY_CREDS.json")
# return response.json(dict(user_creds=user_creds.__dict__, check_if_active=app.url_for('is_active', _scheme='http', _external=True, _server=local_full_address)), 200)
else:
return response.text("Something is wrong with your callback")