HTTP_COOKIE variable not set
Tim Roberts
timr at probo.com
Wed Feb 27 02:33:30 EST 2002
Bevan Koopman <bevank at dstc.edu.au> wrote:
>
>When I run the script below the 'HTTP_COOKIE' environment variable is
>not set, i.e. "HTTP_COOKIE" does not appear in the scripts output:
Of course not. The Cookie module doesn't set any environment variables.
HTTP_COOKIE is set by the web server (for example, Apache or IIS) when it
launches a CGI script in response to a form request from a browser.
The Cookie module is just a shortcut for creating a Set-Cookie header line
to be sent back to the web browser. The next time that browser sends a
request, it will include the cookie in ITS headers; Apache will read those
headers and create the HTTP_COOKIE environment variable for you.
Are you trying to use cookies to communicate between two normal (non-CGI)
Python scripts? That ain't gonna work. There's no easy way to change the
environment of the shell that called you.
>#####################
>import cgi
>import os
>import Cookie
>
>print "Content-Type:text/html\n\n"
>
>cookie = Cookie.SimpleCookie()
>cookie["user"] = the_user
Change this to:
print "Content-Type: text/html"
cookie = Cookie.SimpleCookie()
cookie["user"] = the_user
print cookie
print
That prints the appropriate Set-Cookie header as part of the set of HTTP
headers. It STILL doesn't set the environment variable; that won't happen
until the NEXT request you get from that browser.
>Hence when I run the following script the key is not found
>
>#####################
>import cgi
>import os
>import Cookie
>
>c = Cookie.SimpleCookie()
>if(os.environ.has_key('HTTP_COOKIE')):
> c.load(os.environ['HTTP_COOKIE'])
> print c["user"].value
>else:
> print "no user"
>#####################
>
>How do I correctly create the cookie so I can retrieve in the second
>script.
If that second script is called as a CGI script from the same browser that
receives the page from the FIRST script, it will work.
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list