r.history 返回一个空列表 [python]

huangapple go评论56阅读模式
英文:

r.history returning an empty list [python]

问题

Here is the code in main.py:

import requests

URL = 'localhost:80'
if 'http' not in URL: URL = 'http://' + URL
r = requests.get(URL)
for resp in r.history:
    print(r.url)

Here is the code in webserver.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return '<script>location.href = "https://youtube.com"</script>'

if __name__=='__main__':
    app.run('0.0.0.0', 80)

r.history returns an empty list when I run main.py.

英文:

Here is the code in main.py

import requests

URL = &#39;localhost:80&#39;
if &#39;http&#39; not in URL: URL = &#39;http://&#39;+ URL
r = requests.get(URL)
for resp in r.history:
    print(r.url)

Here is the code in webserver.py

from flask import Flask

app = Flask(__name__)

@app.route(&#39;/&#39;)
def index():
    return &#39;&lt;script&gt;location.href = &quot;https://youtube.com&quot;&lt;/script&gt;&#39;

if __name__==&#39;__main__&#39;:
    app.run(&#39;0.0.0.0&#39;, 80)

r.history returns an empty list when I run main.py

答案1

得分: 0

If you wanna get the redicrection url:

app.py

from flask import Flask, redirect

app = Flask(__name__)

@app.route('/')
def index():
    return redirect('https://python.org')

if __name__=='__main__':
    app.run('0.0.0.0', 80)

req.py

import requests

URL = 'localhost:80'
r = requests.get("http://localhost:80", allow_redirects=True)
print(r.url)

If you wanna get the history of the request

import requests

URL = 'localhost:80'
r = requests.get("http://localhost:80", allow_redirects=True)
print(r.history)
英文:

If you wanna get the redicrection url:

app.py

from flask import Flask, redirect

app = Flask(__name__)

@app.route(&#39;/&#39;)
def index():
    return redirect(&#39;https://python.org&#39;)

if __name__==&#39;__main__&#39;:
    app.run(&#39;0.0.0.0&#39;, 80)

req.py

import requests

URL = &#39;localhost:80&#39;
r = requests.get(&quot;http://localhost:80&quot;, allow_redirects=True)
print(r.url)

If you wanna get the history of the request

import requests

URL = &#39;localhost:80&#39;
r = requests.get(&quot;http://localhost:80&quot;, allow_redirects=True)
print(r.history)

huangapple
  • 本文由 发表于 2023年5月8日 02:57:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定