英文:
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.1
、http://192.168.1.1
、https://192.168.1.1
、http://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
库有任何自动执行此操作的方式。
所以,我建议你:
- 禁用重定向(在你的请求上设置
allow_redirects=False
) - 检查
Response
对象,看看它是否是一个重定向响应(有辅助的@property
可用!) - 解析重定向的 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:
- Disable redirects (set
allow_redirects=False
on your request) - Inspect the
Response
object to see if it was a redirect response (there are helper@property
available!) - 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论