[Tutor] Re: How to extract cookies? (A)

James sendme105@hotmail.com
Fri, 24 Aug 2001 03:39:33 -0700


> Message: 1
> From: "A" <discuss@sendme.cz>
> To: printers@sendme.cz
> Date: Thu, 23 Aug 2001 16:14:41 +0200
> Reply-to: printers@sendme.cz
> Subject: [Tutor] How to extract cookies?
>
> Hi,
> I use httplib module to POST to a website.From  the response(
> header)
> I need to extract cookies. Can you advise me how I can do that
> please?
>
>
> The header I receive looks like
>
> Server: Microsoft-IIS/5.0
> Date: Thu, 23 Aug 2001 12:24:33 GMT
> Location: MemberPortfolio.asp
> Connection: Keep-Alive
> Content-Length: 140
> Content-Type: text/html
> Set-Cookie:
> ECEurope=Password=&Email=leads%40sendme%2Ecz;
> expires=Sun, 21-Oct-2001 23:00:00 GMT; path=/
> Set-Cookie:
> ASPSESSIONIDQQQGQLQO=ABNGDCNBDOIEFJEEPJAOJDKE;
>  path=/
> Cache-control: private
>
> Thank you for help
> Ladislav

I had a similar problem a week or two ago, and based the following entirely
on a suggestion found via google/deja:


This is prolly all you want:

from urllib import *
from mimetools import Message

myurl= urlopen(source_url)
headers=myurl.info()
cookie = headers.getheader("set-cookie")



Here's how I used the info above to urlopen some pages that required cookie
support:

import os, string
from urllib import *
from mimetools import Message

#redefine (override?) urlopen to enable cookie sending
def urlopen(url, data=None, headers=None):
    u = FancyURLopener()
    if headers:
        if type(headers) == type({}):
            headers = headers.items()
        for kw, val in headers:
            u.addheader(kw, val)
    return u.open(url, data)

temppage= urlopen(source_url)
headers=temppage.info()
cookie = headers.getheader("set-cookie")
clist = string.split(cookie, ';')
temppage.close()

# The above gets the cookie info I need...

# to successfully open the page:

page = urlopen(source_url, headers=[('Cookie', clist[0])])

########

Note: I'm new to programming...