[Patches] [ python-Patches-548197 ] Adds cookie support to urllib2.py

noreply@sourceforge.net noreply@sourceforge.net
Mon, 09 Dec 2002 12:50:00 -0800


Patches item #548197, was opened at 2002-04-24 17:31
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=548197&group_id=5470

Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Jeff Pitman (bruthasj)
Assigned to: Jeremy Hylton (jhylton)
Summary: Adds cookie support to urllib2.py

Initial Comment:
Problem    
   
Whenever urllib2 encounters a complex sequence of redirects   
and authentication schemes it is extremely difficult to   
"inject" Cookie headers to each request.   
   
Solution   
   
Make a small change to AbstractHTTPHandler that will detect on   
a host-by-host basis if there are any stored cookies.  This is   
done by using a Dict that is initialized on import of urllib2.    
Therefore the Dict of Hosts is available throughout multiple   
instances of request objects and multiple urlopens on those   
objects.     
   
Upon detection, it will send the Cookies along with the other   
request headers to the host.  If there isn't any, it just   
skips it.    
   
The next thing is to collect the Cookies when we get the   
"reply" from the host.  All of this is done within   
AbstractHTTPHandler -- no other modifications required.   
  
Test  
  
I can login into http://mantisbt.sf.net/ which requires the  
browser to maintain a set of cookies during the session.  
  
   
Future   
   
* Maybe use external files to store and control storage of   
cookies   
* Better API in Cookie module so we don't have to do the 
string.split hack   
* Better API in urllib2 for Cookies (or just make it 
transparent? or both?) 
   
   

----------------------------------------------------------------------

Comment By: John J Lee (jjlee)
Date: 2002-12-09 20:50

Message:
Logged In: YES 
user_id=261020

I just noticed the thread on c.l.py about this, rather late... a day
before that thread started (great minds think alike, apparently),
I released the first version of this:

http://wwwsearch.sourceforge.net/ClientCookie

I intend eventually to get it into a fit state for inclusion in Python.
It's certainly not there yet, but it does work, and is integrated
with urllib2.


----------------------------------------------------------------------

Comment By: Moshe Zadka (moshez)
Date: 2002-06-11 12:22

Message:
Logged In: YES 
user_id=11645

You're giving no control at all to the Python user over
cookies. Additionally, doing it on a per-import
basis is wrong: what if there are two non-co-operating
modules each using urllib2 for their own purposes?. 
I suggest writing a Cookie mixin which people can mixin to
their classes
when they create a manager, which will keep a list of
cookies and so on.

Thanks.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-30 19:20

Message:
Logged In: YES 
user_id=80475

I think this looks good.

Please resubmit patch using the latest version of 
urllib2.py and using string methods instead of the (about 
to be deprecated) string module.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=548197&group_id=5470