Python Requests:如何限制在相同域内的重定向?

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

Python Requests: How to restrict redirects within the same domain?

问题

在Python中,我有以下代码:

session = requests.Session()
session.max_redirects = max_red
response = session.get(ip_address)

但如何限制只允许在相同域名下重定向?我的意思是,如果主请求是发往192.168.1.1,那么我希望允许重定向到192.168.1.1http://192.168.1.1https://192.168.1.1http://sub.192.168.1.1等等...

我不想跟随任何外部重定向,我该如何在Python中做到这一点?

英文:

In python I have:

session = requests.Session()
session.max_redirects = max_red
response = session.get(ip_address)

But how can I limit redirects to same domain only? I mean if the main requests was made to 192.168.1.1 then I want to allow redirections to 192.168.1.1, http://192.168.1.1, https://192.168.1.1, http://sub.192.168.1.1 etc...

I don't want to follow any external redirection, how can I do that in python?

答案1

得分: 2

这是一个未经测试的方法,但这里是为了说明和启发而存在。

我不认为 requests 库有任何自动执行此操作的方式。

所以,我建议你:

  1. 禁用重定向(在你的请求上设置 allow_redirects=False
  2. 检查 Response 对象,看看它是否是一个重定向响应(有辅助的 @property 可用!)
  3. 解析重定向的 URL 并决定是否要跟随该重定向

类似这样的代码:

import requests
from urllib3.util import parse_url

initial_url = "http://192.168.1.1/"

response = requests.get(url=initial_url, allow_redirects=False)

# 如果响应是重定向的
if response.is_redirect:
    redirect_url = response.headers["location"]

    if parse_url(initial_url).host == parse_url(redirect_url).host:
        # 跟随重定向?
        request.get(url=redirect_url, allow_redirects=False)

显然还有更多工作要做(例如递归跟随重定向),但希望这对你有所帮助。

英文:

Note: this is an untested approach, but here for illustration/inspiration

I don't believe the requests library has any way of doing this automagically for you.

So, I propose you:

  1. Disable redirects (set allow_redirects=False on your request)
  2. Inspect the Response object to see if it was a redirect response (there are helper @property available!)
  3. Parse the redirect URL and make your decision as to whether to follow that redirect or not

Something like:

import requests
from urllib3.util import parse_url

intial_url = "http://192.168.1.1/"

response = requests.get(url=intial_url, allow_redirects=False)

# <https://github.com/psf/requests/blob/6e5b15d542a4e85945fd72066bb6cecbc3a82191/requests/models.py##L770-L775>
if response.is_redirect:
    redirect_url = response.headers["location"]

    if parse_url(intial_url).host == parse_url(redirect_url).host:
        # follow redirect?
        request.get(url=redirect_url, allow_redirects=False)

Obviously there's more to do (e.g. recursively follow redirects) but thought it might be useful anyhow

huangapple
  • 本文由 发表于 2023年5月26日 14:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338065.html
匿名

发表评论

匿名网友

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

确定