How to get the true URL of a file on the web. (Python) How to get the true URL of a file on the web. (Python) unix unix

How to get the true URL of a file on the web. (Python)


Use urllib.getUrl()

edit:Sorry, I haven't done this in a while:

import urlliburllib.urlopen(url).geturl()

For example:

>>> f = urllib2.urlopen("http://tinyurl.com/oex2e")>>> f.geturl()'http://www.amazon.com/All-Creatures-Great-Small-Collection/dp/B00006G8FI'>>> 


Mark Pilgrim advises to use httplib2 in "Dive Into Python3" as it handles many things (including redirects) in a smarter way.

>>> import httplib2>>> h = httplib2.Http()>>> response, content = h.request("http://garagaeband.com/3252243")>>> response["content-location"]    "http://garageband.com/michael_jackson4.mp3"


You have to read the response, realize that you got a 302 (FOUND), and parse out the real URL from the response headers, then fetch the resource using the new URI.