Wednesday, September 23, 2009

python: writing your own urllib2 redirect_handler

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):

    def redirect_request(self, req, fp, code, msg, headers, newurl=''):
        global intermediateURL
        intermediateURL = newurl

        if code in (301, 302, 303, "refresh") or (code == 307 and not req.has_data()):
            newRequest = urllib2.Request(   
                                            newurl,
                                            headers=req.headers,
                                            origin_req_host=req.get_origin_req_host(),
                                            unverifiable=True
                                        )
            newRequest._origin_req = getattr(req, "_origin_req", req)
            
        return newRequest
        
        else:
            raise HTTPError(req.get_full_url(), code, msg, headers, fp)

source

No comments: