Skip to content Skip to sidebar Skip to footer

How To Check Redirected Web Page Address, Without Downloading It In Python

For a given url, how can I detect final internet location after HTTP redirects, without downloading final page (e.g. HEAD request.) using python. I am trying to write a mass downlo

Solution 1:

I strongly suggest you to use requests library. It is well coded and actively maintained. Requests can make anything you need like prefetch/

From the Requests' documentation http://docs.python-requests.org/en/latest/user/advanced/ :

By default, when you make a request, the body of the response is downloaded immediately. You can override this behavior and defer downloading the response body until you access the Response.content attribute with the prefetch parameter:

tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'r = requests.get(tarball_url, prefetch=False)

At this point only the response headers have been downloaded and the connection remains open, hence allowing us to make content retrieval conditional:

ifint(r.headers['content-length']) < TOO_LONG:
  content = r.content
  ...

You can further control the workflow by use of the Response.iter_content and Response.iter_lines methods, or reading from the underlying urllib3 urllib3.HTTPResponse at Response.raw

Solution 2:

You can use httplib to send HEAD requests.

Solution 3:

You can also have a look at python-requests, which seems to be the new trendy API for HTTP requests, replacing the possibly awkward httplib2. (see Why Not httplib2)

It also has a head() method for this.

Post a Comment for "How To Check Redirected Web Page Address, Without Downloading It In Python"