Urllib/httplib and cookies

Cliff Crawford cjc26 at nospam.cornell.edu
Thu Aug 9 06:01:38 CEST 2001

* Lad <printers at sendme.cz> menulis:
| Hi Doug,
| Thank you for your email.
| What I need is to automate posting to web sites such as mail.yahoo.com
| (to check email AUTOMATICALY)  that use cookies to sign-in. In Perl I
| can do it very easily. Is it possible to do it in Python as well?

It's possible, but you have to do it yourself (urllib doesn't handle
cookies for you AFAIK).  First you have to get the headers from the
file-like object that urlopen() returns:

>>> import urllib2
>>> f=urllib2.urlopen('http://www.cnn.com/')
>>> f.info().headers
['Server: Netscape-Enterprise/4.1\r\n', 'Date: Thu, 09 Aug 2001 03:29:54
GMT\r\n', 'Set-cookie: CNNid=cf301326-22153-997327795-1;
expires=Wednesday, 30-Dec-2037 16:00:00 GMT; path=/;
domain=.cnn.com\r\n', 'Last-modified: Thu, 09 Aug 2001 03:29:55
GMT\r\n', 'Expires: Thu, 09 Aug 2001 03:30:55 GMT\r\n', 'Cache-control:
private,max-age=60\r\n', 'Content-type: text/html\r\n', 'Connection:
>>> f.close()
>>> cookie = f.info().getheader('Set-cookie')
>>> cookie
'CNNid=cf301326-22153-997327795-1; expires=Wednesday, 30-Dec-2037
16:00:00 GMT; path=/; domain=.cnn.com'

>From now on, you need to send a Cookie header with the cookie data
whenever you make any requests back to the server.  You can do that
by creating an HTTP request object each time with that header:

>>> req = urllib2.Request('http://www.cnn.com/WORLD/')
>>> req.add_header('Cookie', cookie)
>>> f = urllib2.urlopen(req)

Cliff Crawford                           http://www.sowrong.org/
"Today's Goths, who generally tend to be in their teens and 20s,
have nothing to do with the Germanic Visigoths of Europe in the
third and fourth centuries A.D."               -- Jimmy Stewart

More information about the Python-list mailing list