Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def index(request):
"""
聊天页面
"""
template = env.get_template('index.html')
html_content = template.render(title='聊天机器人')
return html(html_content)
if _ENABLE_CB:
user_agent = request.headers.get('user-agent', '')
_os, browser = httpagentparser.simple_detect(user_agent)
breaker = _BREAKERS[browser]
data, status_code = await breaker.run(lambda: _render(request.app.prerender, url, format, proxy))
else:
data, status_code = await _render(request.app.prerender, url, format, proxy)
headers.update({'X-Prerender-Cache': 'miss', 'Last-Modified': formatdate(usegmt=True)})
logger.info('Got %d for %s in %dms',
status_code,
url,
int((time.time() - start_time) * 1000))
if format == 'html':
if 200 <= status_code < 300:
executor.submit(_save_to_cache, url, data.encode('utf-8'), format)
return response.html(
apply_filters(data, HTML_FILTERS),
headers=headers,
status=status_code
)
if 200 <= status_code < 300:
executor.submit(_save_to_cache, url, data, format)
return response.raw(data, headers=headers, status=status_code)
except (asyncio.TimeoutError, asyncio.CancelledError, TemporaryBrowserFailure, RetriesExhausted):
logger.warning('Got 504 for %s in %dms',
url,
int((time.time() - start_time) * 1000))
return response.text('Gateway timeout', status=504)
except TooManyResponseError:
logger.warning('Too many response error for %s in %dms',
url,
int((time.time() - start_time) * 1000))
head_infos = await asyncio.gather(*[
head(session, url) for url in urls
])
if callback and is_valid_callback(callback):
return response.text(
'{}({})'.format(callback, json.dumps(head_infos, indent=2)),
content_type='application/javascript',
headers={'Access-Control-Allow-Origin': '*'},
)
else:
return response.json(
head_infos,
headers={'Access-Control-Allow-Origin': '*'},
)
else:
return response.html(INDEX)
async def apiui_command_help(request, user):
template = env.get_template('apiui_command_help.html')
if use_ssl:
content = template.render(links=await respect_pivot(links, request), name=user['username'], http="https", ws="wss", config=user['ui_config'])
else:
content = template.render(links=await respect_pivot(links, request), name=user['username'], http="http", ws="ws", config=user['ui_config'])
return response.html(content)
async def login(request):
message = ''
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
# for demonstration purpose only, you should use more robust method
if username == 'demo' and password == '1234':
# use User proxy in sanic_auth, this should be some ORM model
# object in production, the default implementation of
# auth.login_user expects User.id and User.name available
user = User(id=1, name=username)
auth.login_user(request, user)
return response.redirect('/')
message = 'invalid username or password'
return response.html(LOGIN_FORM.format(message))
async def index_handler(request):
if CONFIG.fully_configured:
return response.html(jinja.get_template("index.html").render(
phone_number=format_number(CONFIG.phone_number),
host=CONFIG.host))
else:
return response.html(jinja.get_template("env_errors.html").render(
missing_envs=CONFIG.missing_keys))
async def index(request, token):
if token not in TOKENS:
return response.text("Not Found! \nIncorrect token could be used!", 404)
return response.html(INDEX_PAGE)
async def action_ugly(self, request):
"""TBD."""
return html("UGLY")
async def swagger_blueprint_editor_handler(request):
return response.html(self.editor_html)
response_message = "Exception occurred while handling uri: %s"
logger.exception(response_message, url)
if issubclass(type(exception), SanicException):
return text(
"Error: {}".format(exception),
status=getattr(exception, "status_code", 500),
headers=getattr(exception, "headers", dict()),
)
elif self.debug:
html_output = self._render_traceback_html(exception, request)
return html(html_output, status=500)
else:
return html(INTERNAL_SERVER_ERROR_HTML, status=500)