Gof*_*tty 10 python networking flask flask-login
我正在学习使用Flask-login来使登录功能,并且我在下面的教程中面临以下代码:
@app.route('/login', methods = ['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '': # what is it means in this line..?
next_page = url_for('index')
return redirect(next_page)
return render_template('login.html', title='Sign In', form=form)
Run Code Online (Sandbox Code Playgroud)
但是我不确定上面注释的代码是什么..?,尤其是netloc词,那是什么..?,我知道这代表网络局部性,但是该行的目的是什么..?
Inf*_*ake 13
从中RFC 1808, Section 2.1
,每个URL都应遵循特定的格式:
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
Run Code Online (Sandbox Code Playgroud)
该netloc就是第一级域(FLD)表示,该路径之前它来了,并且该方案后。例如,您具有以下URL:
http://www.example.com/index?search=src
Run Code Online (Sandbox Code Playgroud)
在这里,www.example.com
是您的netloc,index
而是路径,search
是查询参数,src
是沿参数传递的值search
。
现在进入您的代码,该if
语句检查是否next_page
存在以及是否next_page
具有netloc,以便可以将用户重定向到index
您网站的(默认)页面。
import urllib.parse
url="https://example.com/something?a=1&b=1"
o = urllib.parse.urlsplit(url)
print(o.netloc)
Run Code Online (Sandbox Code Playgroud)
example.com
归档时间: |
|
查看次数: |
3041 次 |
最近记录: |