本文整理汇总了Python中sanic.response.redirect方法的典型用法代码示例。如果您正苦于以下问题:Python response.redirect方法的具体用法?Python response.redirect怎么用?Python response.redirect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.response
的用法示例。
在下文中一共展示了response.redirect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_redirect_with_params
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def test_redirect_with_params(app, test_str):
use_in_uri = quote(test_str)
@app.route("/api/v1/test/<test>/")
async def init_handler(request, test):
return redirect(f"/api/v2/test/{use_in_uri}/")
@app.route("/api/v2/test/<test>/")
async def target_handler(request, test):
assert test == test_str
return text("OK")
_, response = app.test_client.get(f"/api/v1/test/{use_in_uri}/")
assert response.status == 200
assert response.content == b"OK"
示例2: redirect_login_record_session_google
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def redirect_login_record_session_google(request):
success_cb, error_cb, adapter, first_response = oauth_init(request)
if adapter is None:
return first_response
authomatic = Authomatic(config=CONFIG, secret=SECRET_SALT)
result = authomatic.login(adapter, 'google',
# session=session['authomatic'],
session_saver=lambda: None)
if result:
if result.error:
warning("Google login error", result.error)
return response.redirect('%s?error=%s' % (error_cb, result.error))
elif result.user:
debug("Google login success", result.user)
result.user.update()
debug("Google login update success", result.user)
userdict = result.user.to_dict()
return upsert_login_redirect(request, "google:" + userdict['email'], result.user.to_dict(), success_cb, adapter)
return adapter.response
示例3: reroute_to_refresh
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def reroute_to_refresh(request, resp):
resp.headers['Server'] = server_header
# if you browse somewhere and get greeted with response.json.get('reasons')[0] and "Signature has expired"
if resp and (resp.status == 403 or resp.status == 401) and resp.content_type == "application/json":
output = js.loads(resp.body)
if 'reasons' in output and 'Signature has expired' in output['reasons'][0]:
# unauthorized due to signature expiring, not invalid auth, redirect to /refresh
if request.cookies['refresh_token'] and request.cookies['access_token']:
# auto generate a new
return response.redirect("/uirefresh")
if 'exception' in output and output['exception'] == "AuthenticationFailed":
# authentication failed for one reason or another, redirect them to login
resp = response.redirect("/login")
del resp.cookies['access_token']
del resp.cookies['refresh_token']
return resp
示例4: add_session_to_request
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def add_session_to_request(request):
# before each request initialize a session
# using the client's request
host = request.headers.get('host', None)
user_agent = request.headers.get('user-agent', None)
if user_agent:
user_ip = request.headers.get('X-Forwarded-For')
LOGGER.info('user ip is: {}'.format(user_ip))
if user_ip in CONFIG.FORBIDDEN:
return html("<h3>网站正在维护...</h3>")
if CONFIG.VAL_HOST == 'true':
if not host or host not in CONFIG.HOST:
return redirect('http://www.owllook.net')
if CONFIG.WEBSITE['IS_RUNNING']:
await app.session_interface.open(request)
else:
return html("<h3>网站正在维护...</h3>")
else:
return html("<h3>网站正在维护...</h3>")
示例5: admin_setting
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def admin_setting(request):
user = request['session'].get('user', None)
if user:
try:
motor_db = motor_base.get_db()
data = await motor_db.user.find_one({'user': user})
if data:
return template('admin_setting.html', title='{user}的设置 - owllook'.format(user=user),
is_login=1,
user=user,
register_time=data['register_time'],
email=data.get('email', '请尽快绑定邮箱'))
else:
return text('未知错误')
except Exception as e:
LOGGER.error(e)
return redirect('/')
else:
return redirect('/')
示例6: chapter
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def chapter(request):
"""
返回小说章节目录页
: content_url 这决定当前U页面url的生成方式
: url 章节目录页源url
: novels_name 小说名称
:return: 小说章节内容页
"""
url = request.args.get('url', None)
novels_name = request.args.get('novels_name', None)
netloc = get_netloc(url)
if netloc not in RULES.keys():
return redirect(url)
if netloc in REPLACE_RULES.keys():
url = url.replace(REPLACE_RULES[netloc]['old'], REPLACE_RULES[netloc]['new'])
content_url = RULES[netloc].content_url
content = await cache_owllook_novels_chapter(url=url, netloc=netloc)
if content:
content = str(content).strip('[],, Jjs').replace(', ', '').replace('onerror', '').replace('js', '').replace(
'加入书架', '')
return template(
'chapter.html', novels_name=novels_name, url=url, content_url=content_url, soup=content)
else:
return text('解析失败,请将失败页面反馈给本站,请重新刷新一次,或者访问源网页:{url}'.format(url=url))
示例7: owllook_register
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def owllook_register(request):
"""
用户登录
:param request:
:return:
: -1 用户名或密码不能为空
: 0 用户名或密码错误
: 1 登陆成功
"""
user = request['session'].get('user', None)
if user:
return redirect('/')
else:
ver_que_ans = ver_question()
if ver_que_ans:
request['session']['index'] = ver_que_ans
return template(
'register.html',
title='owllook - 注册 - 网络小说搜索引擎',
question=ver_que_ans[1]
)
else:
return redirect('/')
示例8: index
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def index(request):
# generate a URL for the endpoint `post_handler`
url = app.url_for('post_handler', post_id=5)
# the URL is `/posts/5`, redirect to it
return response.redirect(url)
示例9: handle_request
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def handle_request(request):
return response.redirect('/redirect')
示例10: handler
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def handler(request):
return response.redirect("/sleep/3")
示例11: redirect_app
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def redirect_app(app):
@app.route("/redirect_init")
async def redirect_init(request):
return redirect("/redirect_target")
@app.route("/redirect_init_with_301")
async def redirect_init_with_301(request):
return redirect("/redirect_target", status=301)
@app.route("/redirect_target")
async def redirect_target(request):
return text("OK")
@app.route("/1")
def handler1(request):
return redirect("/2")
@app.route("/2")
def handler2(request):
return redirect("/3")
@app.route("/3")
def handler3(request):
return text("OK")
@app.route("/redirect_with_header_injection")
async def redirect_with_header_injection(request):
return redirect("/unsafe\ntest-header: test-value\n\ntest-body")
return app
示例12: index
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def index(request: Request):
return redirect('/index.html')
# 允许跨域
# @app.middleware('response')
# async def access_control_all_origin(request: Request, response):
# response.headers['Access-Control-Allow-Origin'] = '*'
示例13: index
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def index(request):
return redirect("{}/".format(swagger_blueprint.url_prefix))
示例14: force_https
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def force_https(request):
if DEBUG:
return None
host = request.headers.get("Host", "")
protocol = "https" if request.transport.get_extra_info("sslcontext") else "http"
if request.headers.get("x-forwarded-proto", protocol) == "http":
args = ("https", host, request.path, None, request.query_string, None)
url = urlunparse(args)
return redirect(url)
示例15: upsert_login_redirect
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import redirect [as 别名]
def upsert_login_redirect(request, user_id: str, third_party_info: dict, success_cb: str, adapter):
debug('Login', user_id, 'with', third_party_info, success_cb)
user_or_none: User = User.get('user_id', user_id)
if user_or_none is None:
user_or_none = create_user(user_id=user_id, password=str(token_urlsafe()), third_party_info=third_party_info)
session_obj: Session = Session.create(user_id=user_or_none.user_id)
request['session_id'] = session_obj.session_id
return redirect(success_cb)