How to set a cookie using Cookie Module

T

tryg.olson

Hello -

This is my first attempt at python cookies. I'm using the Cookie
module and trying to set a cookie. Below is my code. The cookie does
not get set. What am I doing wrong?


print "Cache-Control: max-age=0, must-revalidate, no-store"
print "Content-type: text/html"
print
print """
<html><head><title>My Page</title>
<link rel=stylesheet href="/ss.css" type="text/css">"""

c = Cookie.Cookie()
c['MyCookie'] = "Tryg"
print c
print

If I put javascript code to create a cookie in place of my python
cookie code, the javascript cookie gets set.

Any tips appreciated!
Tryg
 
T

tryg.olson

Hello -

This is my first attempt at python cookies. I'm using the Cookie
module and trying to set a cookie. Below is my code. The cookie does
not get set. What am I doing wrong?

print "Cache-Control: max-age=0, must-revalidate, no-store"
print "Content-type: text/html"
print
print """
<html><head><title>My Page</title>
<link rel=stylesheet href="/ss.css" type="text/css">"""

c = Cookie.Cookie()
c['MyCookie'] = "Tryg"
print c
print

If I put javascript code to create a cookie in place of my python
cookie code, the javascript cookie gets set.

Any tips appreciated!
Tryg


I eventually figured it out myself. The cookie code needs to be part
of the HTTP header. See example below.

print "Cache-Control: max-age=0, must-revalidate, no-store"
print "Content-type: text/html"
c = Cookie.Cookie()
c['MyCookie'] = "Tryg"
print c
print
print """
<html><head><title>My Page</title>
<link rel=stylesheet href="/ss.css" type="text/css">"""
 
T

tryg.olson

Hello -

This is my first attempt at python cookies. I'm using the Cookie
module and trying to set a cookie. Below is my code. The cookie does
not get set. What am I doing wrong?

print "Cache-Control: max-age=0, must-revalidate, no-store"
print "Content-type: text/html"
print
print """
<html><head><title>My Page</title>
<link rel=stylesheet href="/ss.css" type="text/css">"""

c = Cookie.Cookie()
c['MyCookie'] = "Tryg"
print c
print

If I put javascript code to create a cookie in place of my python
cookie code, the javascript cookie gets set.

Any tips appreciated!
Tryg


I eventually figured it out myself. The cookie code needs to be part
of the HTTP header. See example below.

print "Cache-Control: max-age=0, must-revalidate, no-store"
print "Content-type: text/html"
c = Cookie.Cookie()
c['MyCookie'] = "Tryg"
print c
print
print """
<html><head><title>My Page</title>
<link rel=stylesheet href="/ss.css" type="text/css">"""
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top