? on working with http redirects in htmllib

Paul Robinson paul.robinson at quantisci.co.uk
Tue Nov 30 18:59:54 CET 1999


Levi Cook wrote:
> 
> Does anyone know how to process and follow a redirect in python?
> 
> I can easily tell if I've recieved the 301 or 302 errcode, but I cannot for
> the life of me figure out how to get the URL of the new destination.

The URL is simply in the "Location" header.

so for example (untested code):

def actualGet(server, uri):
        got = 0
        while not got:
            h = httplib.HTTP(server)
            h.putrequest('GET', uri)
            h.putheader('Accept', 'text/html')
            h.endheaders()
            errcode, errmsg, headers = h.getreply()
            if errcode == 200:
                f = h.getfile()
                got = 1
            elif errcode == 302 or errcode == 301:
                url = headers.getheader('location')
                #Split into uri and server
                URL2Fetch = urlparse.urlparse(address)
                server = URL2Fetch[1]
                uri = URL2Fetch[2]
            elif errcode == 404:
		#Do something else!
                got = 1
            else:
                raise RuntimeError, ('Unsupported errcode', errcode,
errmsg)
        return f

Hope this helps,
	Paul.




More information about the Python-list mailing list