? on working with http redirects in htmllib

Levi Cook levicook at earthlink.net
Tue Nov 30 12:20:06 EST 1999


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.

Any help is greatly appreciated.
- Levi Cook

##### code snippet that i use to retrieve pages...

import httplib, urllib

address = 'http://www.python.org/doc/current/lib/node227.html'

URL2Fetch = urlparse.urlparse( address )

# fetch page
h = httplib.HTTP(URL2Fetch[1])
h.putrequest('GET', URL2Fetch[2])
h.putheader('Accept', 'text/html')
h.putheader('Accept', 'text/plain')
h.endheaders()
errcode, errmsg, headers = h.getreply()

# script only knows how to process html
if not headers.gettype() == 'text/html':
     raise redirect(address)

f = h.getfile()
data = f.read() # Get the raw HTML
f.close()








More information about the Python-list mailing list