response.history的顺序是什么?
What's the order of response.history?
假设我向 A 发送了一个 HTTP 请求,该请求重定向到 B,然后是 C。
response = requests.get(A_url, allow_redirects = True)
据我了解response的内容是A.
但是response.history里面的顺序是什么?是 [B,C] 还是 [C,B]?
从使用 hist.append(resp)
的 requests
source code 开始,它看起来按照看到的顺序“升序”(顺序)排序。因此,[A, B]
来自您的示例。
hist = [] # keep track of history
url = self.get_redirect_target(resp)
previous_fragment = urlparse(req.url).fragment
while url:
prepared_request = req.copy()
# Update history and keep track of redirects.
# resp.history must ignore the original request in this loop
hist.append(resp)
resp.history = hist[1:]
...
这是来自 .resolve_redirects()
的一段代码,它一直在寻找重定向,直到它不再被重定向。 . get_redirect_target()
,反过来,while stop returning a URL(它将return None
)如果没有重定向目标,结束while url
循环如上所示。
一个可重现的例子
创建以下 Flask 应用程序:
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route("/a")
def a():
return redirect(url_for('b'))
@app.route("/b")
def b():
return redirect(url_for('c'))
@app.route("/c")
def c():
return "<p>Hello, C!</p>"
上菜:
$ python3 -m flask run
现在发送请求:
>>> import requests
>>> resp = requests.get("http://127.0.0.1:5000/a")
>>> resp.history
[<Response [302]>, <Response [302]>]
>>> [x.url for x in resp.history]
['http://127.0.0.1:5000/a', 'http://127.0.0.1:5000/b']
>>> resp.url
'http://127.0.0.1:5000/c'
假设我向 A 发送了一个 HTTP 请求,该请求重定向到 B,然后是 C。
response = requests.get(A_url, allow_redirects = True)
据我了解response的内容是A.
但是response.history里面的顺序是什么?是 [B,C] 还是 [C,B]?
从使用 hist.append(resp)
的 requests
source code 开始,它看起来按照看到的顺序“升序”(顺序)排序。因此,[A, B]
来自您的示例。
hist = [] # keep track of history
url = self.get_redirect_target(resp)
previous_fragment = urlparse(req.url).fragment
while url:
prepared_request = req.copy()
# Update history and keep track of redirects.
# resp.history must ignore the original request in this loop
hist.append(resp)
resp.history = hist[1:]
...
这是来自 .resolve_redirects()
的一段代码,它一直在寻找重定向,直到它不再被重定向。 . get_redirect_target()
,反过来,while stop returning a URL(它将return None
)如果没有重定向目标,结束while url
循环如上所示。
一个可重现的例子
创建以下 Flask 应用程序:
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route("/a")
def a():
return redirect(url_for('b'))
@app.route("/b")
def b():
return redirect(url_for('c'))
@app.route("/c")
def c():
return "<p>Hello, C!</p>"
上菜:
$ python3 -m flask run
现在发送请求:
>>> import requests
>>> resp = requests.get("http://127.0.0.1:5000/a")
>>> resp.history
[<Response [302]>, <Response [302]>]
>>> [x.url for x in resp.history]
['http://127.0.0.1:5000/a', 'http://127.0.0.1:5000/b']
>>> resp.url
'http://127.0.0.1:5000/c'