Cookies
Victor Subervi
victorsubervi at gmail.com
Wed Dec 30 10:27:39 EST 2009
On Wed, Dec 30, 2009 at 9:56 AM, Carsten Haese <carsten.haese at gmail.com>wrote:
> So, guess again. The "trivial example" has three more lines of code. One
> of them is unlike any line you have in your code. That's the line
> responsible for producing the "Set-Cookie" header, and that's the line
> you're missing.
>
So you're suggesting I somehow execute this code:
python Cookie_setheaders.py
in my script? I presume I'd change "Cookie_setheaders.py" to my file name.
That doesn't make sense to me.
I'm sorry but I'm lost. The page on which you said had the missing line:
http://www.doughellmann.com/PyMOTW/Cookie/index.html
has the following code:
import Cookie
c = Cookie.SimpleCookie()
c['mycookie'] = 'cookie_value'
print c
All of which is in my code. The tutorial gave this printout:
$ python Cookie_setheaders.py
Set-Cookie: mycookie=cookie_value
It also has this code:
import Cookie
import datetime
def show_cookie(c):
print c
for key, morsel in c.iteritems():
print
print 'key =', morsel.key
print ' value =', morsel.value
print ' coded_value =', morsel.coded_value
for name in morsel.keys():
if morsel[name]:
print ' %s = %s' % (name, morsel[name])
which, when I tried it, printed nothing at all. Not a very good tutorial :(
This page
http://webpython.codepoint.net/cgi_set_the_cookie
has the following code:
#!/usr/bin/env python
import time
print 'Set-Cookie: lastvisit=' + str(time.time());
print 'Content-Type: text/html\n'
print '<html><body>'
print 'Server time is', time.asctime(time.localtime())
print '</body></html>'
#!/usr/bin/env python
import time, Cookie
cookie = Cookie.SimpleCookie()
cookie['lastvisit'] = str(time.time())
print cookie
print 'Content-Type: text/html\n'
print '<html><body>'
print 'Server time is', time.asctime(time.localtime())
print '</body></html>'
all of which is included in my code (except for extraneous print
statements). Here again is my code:
#! /usr/bin/python
import string
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
import datetime, Cookie, random
from particulars import title
from templateFrame import top, bottom
from particulars import myCookie
import time
import fpformat
from sets import Set
from particulars import ourOptions
def cart():
print '''Content-Type: text/html\r\n
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
'''
cookie = os.environ.has_key('HTTP_COOKIE')
if not cookie:
cookie = Cookie.SimpleCookie()
cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
cookie['lastvisit'] = str(time.time())
cookie['lastvisit']['expires'] = cExpires
cookie['lastvisit']['path'] = cPath
cookie['lastvisit']['comment'] = cComment
cookie['lastvisit']['domain'] = cDomain
cookie['lastvisit']['max-age'] = cMaxAge
cookie['lastvisit']['version'] = cVersion
cookie['mycookie'] = 'mycookie'
cookieFlag = 'new'
else:
cookie = Cookie.SimpleCookie(cookie)
cookieFlag = 'old'
# Don't know what to do with this. It's for when client won't accept
cookies
# sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
# session = shelve.open(sessionDir + '/sess_' + sid, writeback=True)
# session['lastvisit'] = repr(time.time())
# session.close()
print cookieFlag
cookie.load(cookie)
print cookie
...
Please advise.
TIA,
beno
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091230/f9205786/attachment-0001.html>
More information about the Python-list
mailing list