Python HTTP HEAD - 正确处理重定向? [英] Python HTTP HEAD - dealing with redirects properly?

查看:48
本文介绍了Python HTTP HEAD - 正确处理重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 urllib2 发出 HEAD 请求,如下所示:

I can use urllib2 to make HEAD requests like so:

import urllib2
request = urllib2.Request('http://example.com')
request.get_method = lambda: 'HEAD'
urllib2.urlopen(request)

问题是当它跟随重定向时,它使用 GET 而不是 HEAD.

The problem is that it appears that when this follows redirects, it uses GET instead of HEAD.

这个 HEAD 请求的目的是检查我将要下载的 URL 的大小和内容类型,以便我可以确保我不会下载一些巨大的文档.(URL 由随机互联网用户通过 IRC 提供).

The purpose of this HEAD request is to check the size and content type of the URL I'm about to download so that I can ensure that I don't download some huge document. (The URL is supplied by a random internet user through IRC).

如何在跟踪重定向时使用 HEAD 请求?

How could I make it use HEAD requests when following redirects?

推荐答案

好问题!如果您开始使用 urllib2,您需要查看 这个答案 关于构建您自己的重定向处理程序.

Good question! If you're set on using urllib2, you'll want to look at this answer about the construction of your own redirect handler.

简而言之(阅读:公然从上一个答案中窃取):

In short (read: blatantly stolen from the previous answer):

import urllib2

#redirect_handler = urllib2.HTTPRedirectHandler()

class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        print "Cookie Manip Right Here"
        return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)

    http_error_301 = http_error_303 = http_error_307 = http_error_302

cookieprocessor = urllib2.HTTPCookieProcessor()

opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor)
urllib2.install_opener(opener)

response =urllib2.urlopen("WHEREEVER")
print response.read()

print cookieprocessor.cookiejar

此外,如勘误中所述,您可以使用 Python Requests.

Also, as mentioned in the errata, you can use Python Requests.

这篇关于Python HTTP HEAD - 正确处理重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆