[urllib2] 302 -> can't get cookie

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jan 13 08:04:56 CET 2009


En Wed, 07 Jan 2009 09:27:00 -0200, Gilles Ganault <nospam at nospam.com>  
escribió:

> I'm using urllib2 to connect to a web server with POST, and then the
> server sends a cookie to hold the session ID, but also redirects the
> user to another page:

If you're still interested, the way to avoid a redirect would be to use a  
RedirectHandler that does nothing:

 from urllib2 import HTTPRedirectHandler, build_opener

class AvoidRedirectHandler(HTTPRedirectHandler):
     def http_error_302(self, req, fp, code, msg, headers): pass
     http_error_301 = http_error_303 = http_error_307 = http_error_302

opener = build_opener(AvoidRedirectHandler)
opener.open(url, data)

-- 
Gabriel Genellina




More information about the Python-list mailing list