More than one cookie with urllib2

R

Rafael T. Ugolini

Im trying to add more than one cookie with urllib2 but im not getting
much sucess.
An example...

urllib2.Request('http://localhost','j_username=psyca&j_password=******',{
'Cookie':'sab=contextUrl+https%3A%2F%2Fwwws.vexbr.com.br%2Fcafedev%2F%7CnasIp+10.50.0.3%7CneedCheckCode+false%7ClocationId+85%7CautoLogout+false%7CserviceProviderId+1%7CorigUrl+%2Findex.html%7CwebContext+cafedev%7CclientIp+192.168.1.107%7CnasId+vex_cafedev%7CloginType+0%7Cclass+class+com.wificom.sab.aaa.dto.SessionDTO%7CloginResumed+false%7CloggedIn+false%7Clanguage+en%7CpricingPlanId+0%7CsslPort+443%7CroamingPriceId+0%7CclientMac+00%3A07%3A95%3AD3%3A0C%3A7C%7CserverUrl+https%3A%2F%2Fwwws.vexbr.com.br%2F',
'Cookie':'JSESSIONID=DFA58D39389BA5E3CEB5C92F9308792C',
'Content-type':'application/x-www-form-urlencoded',
'Content-length':str(len('j_username=psyca&j_password=******'))})

nebula:/home/nebuloso# nc -l -p 80
POST / HTTP/1.0
Host: localhost
User-agent: Python-urllib/2.1
Cookie: JSESSIONID=DFA58D39389BA5E3CEB5C92F9308792C
Content-length: 34
Content-type: application/x-www-form-urlencoded

j_username=psyca&j_password=******
 
J

John J. Lee

Rafael T. Ugolini said:
Im trying to add more than one cookie with urllib2 but im not getting
much sucess.
An example...

urllib2.Request('http://localhost','j_username=psyca&j_password=******',{
'Cookie':'sab=contextUrl+https%3A%2F%2Fwwws.vexbr.com.br%2Fcafedev%2F%7CnasIp+10.50.0.3%7CneedCheckCode+false%7ClocationId+85%7CautoLogout+false%7CserviceProviderId+1%7CorigUrl+%2Findex.html%7CwebContext+cafedev%7CclientIp+192.168.1.107%7CnasId+vex_cafedev%7CloginType+0%7Cclass+class+com.wificom.sab.aaa.dto.SessionDTO%7CloginResumed+false%7CloggedIn+false%7Clanguage+en%7CpricingPlanId+0%7CsslPort+443%7CroamingPriceId+0%7CclientMac+00%3A07%3A95%3AD3%3A0C%3A7C%7CserverUrl+https%3A%2F%2Fwwws.vexbr.com.br%2F',
'Cookie':'JSESSIONID=DFA58D39389BA5E3CEB5C92F9308792C',
'Content-type':'application/x-www-form-urlencoded',
'Content-length':str(len('j_username=psyca&j_password=******'))})
[...]

Blech.


and another thing , headers is a dic , you cant add 2 equal keys...
dunno how to do it.
[...]

If you want to stay sane, use this:

http://wwwsearch.sourceforge.net/ClientCookie

just say:

ClientCookie.urlopen(req)


and forget about cookies. If you insist on doing it by hand, though:
you need to join your key=value pairs with semicolons:

Cookie: foo=bar; spam=eggs


John
 
P

Peter Hansen

John J. Lee said:
If you want to stay sane, use this:

http://wwwsearch.sourceforge.net/ClientCookie

just say:

ClientCookie.urlopen(req)

and forget about cookies.

As the author, John might seem biased, but I'll weigh in with my own
praise for the product. His comment "if you want to stay sane" is
really on-the-mark here... Cookies _are_ insane, but ClientCookie at
least provides them a nice padded room in which to reside...

Thanks, John! :)

-Peter
 
P

Paul Rubin

Peter Hansen said:
As the author, John might seem biased, but I'll weigh in with my own
praise for the product. His comment "if you want to stay sane" is
really on-the-mark here... Cookies _are_ insane, but ClientCookie at
least provides them a nice padded room in which to reside...

Cookies aren't insane in concept, but their implementation leaves some
things to be desired.
 
P

Peter Hansen

Paul said:
Cookies aren't insane in concept, but their implementation leaves some
things to be desired.

Agreed... especially with IE6.1, where I still have an incredibly annoying
problem with a Zope-based Intranet where my cookies are being forgotten
periodically, which among other things has led to repeated data loss
(when combined with anothe IE peculiarity which loses the data in the input
fields of a form if the form submission fails... when you hit "back" you
no longer have your data to resubmit).

Oh, but I'm describing a problem with a _Microsoft_ product, so the whole
comment was redundant. Sorry.

-Peter
 
J

John J. Lee

Peter Hansen said:
Agreed... especially with IE6.1, where I still have an incredibly annoying
problem with a Zope-based Intranet where my cookies are being forgotten
periodically, which among other things has led to repeated data loss
[...]

Do you know why / when / which? Are these cookies in your browser or
your users' browsers? Third party? Session or persistent?

I ask because browser behaviour is the only standard in use, really.
Guess which browser?

Of course, cookies are "allowed" to vanish at random times. I
remember reading something about servers telling browsers which
cookies are important to users and shouldn't be thrown away, but I've
completely forgotten where that was...


John
 
P

Peter Hansen

John J. Lee said:
Peter Hansen said:
Agreed... especially with IE6.1, where I still have an incredibly annoying
problem with a Zope-based Intranet where my cookies are being forgotten
periodically, which among other things has led to repeated data loss
[...]

Do you know why / when / which? Are these cookies in your browser or
your users' browsers? Third party? Session or persistent?

Unfortunately, I don't know any of the above or I would definitely have
fixed it by now. It's intermittent, though at least after two years I've
found a way to reproduce it fairly reliably with an adequate number of
clicks between a particular set of links I use often. The cookies that
are lost are both session and persistent: in fact all cookies pertaining
to one particular domain are lost simultaneously, it seems. My own
browser (IE). Using a home-brew issue tracker that was written largely
with DTML about three years ago... at the moment I'm still assuming
it's a problem only with that product, but it does not affect Mozilla
at all so it's clearly linked to IE-specific behaviour.
Of course, cookies are "allowed" to vanish at random times. I
remember reading something about servers telling browsers which
cookies are important to users and shouldn't be thrown away, but I've
completely forgotten where that was...

Hmm.... I doubt they're supposed to vanish if there is not too much
data stored in them, and as there are no reports that I can find on the
web about similar behaviour, I suspect they _don't_ vanish, even if they're
allowed to.

-PEter
 
J

John J. Lee

Peter Hansen said:
Hmm.... I doubt they're supposed to vanish if there is not too much
data stored in them, and as there are no reports that I can find on the

I guess the only way to find out would be to find/write a little proxy
that keeps the most recent N HTTP messages, so you can look back at
what happened after they vanish.

web about similar behaviour, I suspect they _don't_ vanish, even if they're
allowed to.

You mean maybe the server deletes them by mistake? Could be.


John
 

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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top