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

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

r.history returning an empty list [python]

问题

Here is the code in main.py:

  1. import requests
  2. URL = 'localhost:80'
  3. if 'http' not in URL: URL = 'http://' + URL
  4. r = requests.get(URL)
  5. for resp in r.history:
  6. print(r.url)

Here is the code in webserver.py:

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. return '<script>location.href = "https://youtube.com"</script>'
  6. if __name__=='__main__':
  7. 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

  1. import requests
  2. URL = &#39;localhost:80&#39;
  3. if &#39;http&#39; not in URL: URL = &#39;http://&#39;+ URL
  4. r = requests.get(URL)
  5. for resp in r.history:
  6. print(r.url)

Here is the code in webserver.py

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route(&#39;/&#39;)
  4. def index():
  5. return &#39;&lt;script&gt;location.href = &quot;https://youtube.com&quot;&lt;/script&gt;&#39;
  6. if __name__==&#39;__main__&#39;:
  7. 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

  1. from flask import Flask, redirect
  2. app = Flask(__name__)
  3. @app.route('/')
  4. def index():
  5. return redirect('https://python.org')
  6. if __name__=='__main__':
  7. app.run('0.0.0.0', 80)

req.py

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

If you wanna get the history of the request

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

If you wanna get the redicrection url:

app.py

  1. from flask import Flask, redirect
  2. app = Flask(__name__)
  3. @app.route(&#39;/&#39;)
  4. def index():
  5. return redirect(&#39;https://python.org&#39;)
  6. if __name__==&#39;__main__&#39;:
  7. app.run(&#39;0.0.0.0&#39;, 80)

req.py

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

If you wanna get the history of the request

  1. import requests
  2. URL = &#39;localhost:80&#39;
  3. r = requests.get(&quot;http://localhost:80&quot;, allow_redirects=True)
  4. 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:

确定