Skip to main content
 首页 » 编程设计

python之如何在 HTTPConnectionPool urllib3 中进行几次重定向后获取响应历史记录

2024年10月25日14over140

我正在使用 urllib3 的 HTTPConnectionPool 进行发布请求。这里的代码 http://something.com/file.php 有 2 重定向。请求完成后,我在 headers 和 data 中得到最终响应。如何查看服务器同时进行的重定向历史记录?

headers = {.....} 
data = {...} 
newPool = urllib3.HTTPConnectionPool("something.com",port=80,maxsize=5,headers=headers,retries =5,timeout=10) 
r = newPool.request('POST', '/file.php',fields=data,redirect =True) 
print r.ststus 
print r.headers 
print r.data 

请给我一些建议。提前致谢。

请您参考如下方法:

不幸的是,这还没有内置到 urllib3 中。我有 open an issue (#576)添加此功能。

现在,要获取重定向历史记录,您需要管理自己的重定向或者可以使用 requests它在下面使用 urllib3 和 implements this feature .