Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a page is being redirected

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!

like image 541
bgoncalves Avatar asked Dec 01 '08 19:12

bgoncalves


2 Answers

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')
like image 109
Adam Rosenfield Avatar answered Oct 12 '22 06:10

Adam Rosenfield


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.

like image 20
S.Lott Avatar answered Oct 12 '22 08:10

S.Lott