[urllib2] 302 -> can't get cookie

G

Gilles Ganault

Hello

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:

===
HTTP/1.1 302 Found
Date: Wed, 07 Jan 2009 11:20:51 GMT
Server: Apache
Set-Cookie: PHPSESSID=4015f14eb04dc81159253a9533a7c590; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
Pragma: no-cache
Location: second_page.php
Content-Length: 0
Connection: close
Content-Type: text/html; charset=ISO-8859-1
===

As a result, urllib2 follows this new page, and the HTTP header that I
get is the one from the second page.

Is there a way to ignore this second page, and stick to the first page
through which I connect?

Thank you.
 
G

Gabriel Genellina

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top