Newbie still struggeling with Cookies
David Lees
DavidL.no.no.spamno at raqia.com
Mon Nov 20 15:35:38 EST 2000
It works. Great. Thank you very much.
I am a bit puzzled how it works. I did not change the line:
CGI_HANDLER = "/cgi-bin/test.py"
and I have nothing called "test.py" in my cgi-bin directory. I called
the code below "TestCookie.py" and executed directly from my Netscape
4.75 browser.
david lees
szhao wrote:
>
> The following is part of CGI scripts I stole from GvR (FAQWiz.py)
>
> #!C:/progra~1/python/python.exe
>
> import os, time, string
> CGI_HANDLER = "/cgi-bin/test.py"
>
> def send_cookie(name, value, lifetime):
> """Send Cookie to browser.
> name: the name of the cookie to be set
> value: The value assigned to the named cookie
> lifetime: Cookie expiration in seconds.
> """
> import urllib
> value = urllib.quote(value)
> then = time.time() + lifetime
> gmt = time.gmtime(then)
> path = os.environ.get('SCRIPT_NAME', CGI_HANDLER)
> print "Set-Cookie: %s=%s; path=%s;" % (name, value, path),
> print time.strftime("expires=%a, %d-%b-%y %X GMT", gmt)
>
> def load_cookies():
> if not os.environ.has_key('HTTP_COOKIE'):
> return {}
> raw = os.environ['HTTP_COOKIE']
> words = map(string.strip, string.split(raw, ';'))
> cookies = {}
> for word in words:
> i = string.find(word, '=')
> if i >= 0:
> key, value = word[:i], word[i+1:]
> cookies[key] = value
> return cookies
>
> def get_cookie(name):
> cookies = load_cookies()
> try:
> value = cookies[name]
> except KeyError:
> return ""
> import urllib
> return urllib.unquote(value)
>
> def _test():
> """show the cookie set at the second time this script is hit."""
> cookie = get_cookie("MY_COOKIE")
> if not cookie: #first time
> send_cookie("MY_COOKIE", "COOKIE-0001", 30*60)
> print "Content-Type: text/html\n\n"
> print "<pre>%s</pre>" % cookie
>
> _test()
>
> "David Lees" <DavidL.nono.spam.nono at raqia.com> wrote in message
> news:3A158D94.52E50B3A at raqia.com...
> > I am trying to learn Python and use it for some simple CGI scripts on an
> > Apache server. I have gotten up the hello world type scripts and simple
> > parsing of form (by following the form.py example in the book by Altom).
> > I am stuck on setting a cookie. The local perl guru tells me that there
> > is a perl module called "cgi.pm" that lets him do all this stuff easily
> > and quickly. I have looked at Guido's example on a viewgraph at:
> > www.python.org/doc/essays/ppt/sd99east/tsld057.htm
> > but it is not clear to me how to use it. For one thing it references
> > "test.py" and I assume that the Cookie example also resides in the
> > cgi-bin, but the HTML code that refereces the unnamed Cookie example
> > seems to be unavailable.
> >
> > I also have found an article by Kuchling title "A CGI Framework in
> > Python", which includes code that looks very useful, but I am not sure
> > how to install it all.
> >
> > My questions are:
> > 1. Is there a simple, complete cookie example for Python, understandable
> > by a newbie?
> > 2. Is all the functionality of the perl cgi.pm module available in
> > Python?
> >
> > Thanks in advance.
> >
> > david lees
> >
> >
> >
More information about the Python-list
mailing list