I need to check whether a page is being redirected or not without actually downloading the content. I just need the final URL. What's the best way of doing this is Python? Thanks!
Asked
Active
Viewed 3,406 times
2 Answers
9
If you specifically want to avoid downloading the content, you'll need to use the HEAD request method. I believe the urllib
and urllib2
libraries do not support HEAD requests, so you'll have to use the lower-level httplib
library:
import httplib
h = httplib.HTTPConnection('www.example.com')
h.request('HEAD', '/')
response = h.getresponse()
// Check for 30x status code
if 300 <= response.status < 400:
// It's a redirect
location = response.getheader('Location')

Adam Rosenfield
- 390,455
- 97
- 512
- 589
-
Great. I was trying to force urllib/urllib2 to do this without much luck, and httplib's documentation isn't the best. Thanks! – bgoncalves Dec 01 '08 at 20:15
-
@Adam It looks like you can do it with `urllib2`. See [here](http://stackoverflow.com/questions/4421170/python-head-request-with-urllib2). – pushkin Jan 04 '16 at 02:20
1
When you open the URL with urllib2
, and you're redirected, you get a status 30x for redirection. Check the info to see the location to which you're redirected. You don't need to read the page to read the info()
that's part of the response.

S.Lott
- 384,516
- 81
- 508
- 779
-
Does urllib2 give you a way to issue a HEAD command? That's usually the way to get just the information you need without the network overhead of transmitting the page contents. – Paul Tomblin Dec 01 '08 at 19:17
-
You don't have to read the page. Your response includes a socket which you can simply close. – S.Lott Dec 01 '08 at 19:29
-
Yes, but you're still incurring network traffic. The point of HEAD is to not incur the network traffic. – Adam Rosenfield Dec 01 '08 at 19:32