5

I am new in python cgi script. I want to read cookie in python. I tried following code:

from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib

#Create a CookieJar object to hold the cookies
cj = cookielib.CookieJar()

#Create an opener to open pages using the http protocol and to process cookies.
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())

#Check out the cookies
print "the cookies are: "
for cookie in cj:
    print cookie

But, I see only the cookies are: msg.

Am I doing something wrong?

falsetru
  • 357,413
  • 63
  • 732
  • 636
rpdev123
  • 218
  • 2
  • 3
  • 7
  • possible duplicate of [Retrieving all Cookies in Python](http://stackoverflow.com/questions/921532/retrieving-all-cookies-in-python) – Ian Mackinnon Jul 31 '14 at 12:54

2 Answers2

7

Try this to read cookie in python:

#!/usr/bin/python

import os

# Hello world python program
print "Content-Type: text/html;charset=utf-8";
print

handler = {}
if 'HTTP_COOKIE' in os.environ:
    cookies = os.environ['HTTP_COOKIE']
    cookies = cookies.split('; ')

    for cookie in cookies:
        cookie = cookie.split('=')
        handler[cookie[0]] = cookie[1]

for k in handler:
    print k + " = " + handler[k] + "<br>
Mosiur
  • 1,342
  • 13
  • 16
0

If you don't use opener, the cookie jar is not populated.

Access webpage that issue Set-Cookie header.

For example:

from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib

#Create a CookieJar object to hold the cookies
cj = cookielib.CookieJar()

#Create an opener to open pages using the http protocol and to process cookies.
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())

response = opener.open('http://google.com/') # <---
response.read()

#Check out the cookies
print "the cookies are: "
for cookie in cj:
    print cookie

prints

the cookies are:
<Cookie NID=67=aBkBw0UEgv... for .google.co.kr/>
<Cookie PREF=ID=b99fae87d... for .google.co.kr
<Cookie NID=67=c8QgK_rfyf... for .google.com/>
<Cookie PREF=ID=dbb574e7d... for .google.com/>
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Still only "the cookies are: " message is appearing. re "opener.open('http://google.com/')", here "http://google.com/" will be my website. Correct? – rpdev123 Jan 03 '14 at 07:55
  • @rpdev123, Does your website output `Set-Cookie` header ? Try first with `http://google.com/` first. Then try your website url. (Make sure your website produce cookies.) – falsetru Jan 03 '14 at 07:57
  • Still not working. re "Does your website output Set-Cookie header ?", what to do with this? – rpdev123 Jan 03 '14 at 08:01
  • @rpdev123, You can check Http headers using browser's debugging facility like `Developing Tools - Network tab` in Chrome / `Net` tab in Firebug. – falsetru Jan 03 '14 at 08:02
  • Yes, I can see Cookie in Header in Firebug net tab. Here is cookie info in header i can see in firebug net tab: Cookie __utma=150254709.575910388.1384152185.1386075091.1386077854.6; __utmz=150254709.1384152185.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __zlcmid=MKdN8V0sE0SPFc; gpv_p23=no%20value; s_nr=1388401890342-Repeat; EuE=gs3t32u978nqsubopcrmifioi2; affiliateID_1=100; merchantID_1=1; merchantPromoID_1=1; – rpdev123 Jan 03 '14 at 08:11
  • @rpdev123, Did you mean you're writing cgi script? ;) – falsetru Jan 03 '14 at 08:50