473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Cookie not reading first time though...

I'm trying to learn how to use python for cgi scripts...

I created a form that submits some info that I put in a cookie. Then
the script calls itself and reads the cookie, and displays a different
form to get more information.

The problem I'm running into is the script is setting the cookie... but
it doesn't read it the first time through for some reason, if I
immediately try it again it works fine. So for some reason it's not
picking up the cookie the first time.

Would it be a better idea to break the script into two different scripts
to accomplish this?

Any advise would be appreciated, please be gentle, I'm still a newbie
;-)

here is the script:

#!/usr/bin/python

import cgi,os,Cookie
import cgitb; cgitb.enable()

def name_race():
print "Content-Type: text/html\n\n"
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"
\"http://www.w3.org/TR/html4/strict.dtd\">"
print "<html>"
print "<body>"
print "<form method = post ACTION =\"test.py\"> "
print "<table><tr>"
print "<td><b>Name:</b></td><td><input type=text name=\"name\"></td>
</tr>"
print "</table>"
print "<p>"
print "<table>"
print "<tr>"
print "<td><b>Race:</b></td></tr>"
print "<tr>"
print "<td>Human</td><td><input type=radio name=\"race\" value=\"1
\"></td></tr>"
print "<tr>"
print "<td>Elf</td><td><input type=radio name=\"race\" value=\"2\">
</td></tr>"
print "</table>"
print "<p>"
print "<input type = hidden name=\"action\" value=\"getclass\">"
print "<input type = submit value =\"Enter\">"
def pick_class():
mycookie = Cookie.SmartCookie(os.environ.get("HTTP_COOKIE", ""))
race = mycookie["race"].value
name = mycookie["name"].value
print "Content-Type: text/html\n\n"
print "<html>"
print "<body>"
print name
print race
print "</body>\n</html>"

def print_sheet():
pass
def main():
form = cgi.FieldStorage()
mycookie = Cookie.SmartCookie()
if form.has_key("name") and form.has_key("race"):
if (form["action"].value == "getclass"):
mycookie["name"] = form["name"].value
mycookie["name"]["max-age"] = 60
mycookie["race"] = form["race"].value
mycookie["race"]["max-age"] = 60
print mycookie

pick_class()
else: name_race()

main()
Jul 18 '05 #1
0 1083

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: N.K | last post by:
Hi , Python's existing cookie module doesnt supports new cookie headers SetCookie2 , How to submit a patch for that ? I tried emailing person who owns that module.But no response. Thanks,...
2
by: Michael | last post by:
I am reading and setting a cookie using JavaScript in the BODY onload and onunload events respectively. This works fine. However when I use ASP to set the cookie under some condition where I...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
4
by: Gridlock | last post by:
I'm trying to read the cookies using HttpContext.Current.Request.Cookies, but the only cookie that I get is the ASP.NET SessionId cookie. There are many cookies on the machine, why am I only...
9
by: Mike Reed | last post by:
I must be having a "senile" day! I cannot recall, nor get to work, code to read a cookie's expiration date/time in an ASP page/VBScript. What am I missing? *** Sent via Developersdex...
2
by: rn5a | last post by:
A web.config file has the following code: <configuration> <system.web> <authentication mode="Forms"> <forms name="NETConnectCookie" loginUrl="Login.aspx"> <credentials passwordFormat="SHA1"/>...
3
by: Dan | last post by:
Hi, I am trying to refresh the cookie to make sure the timeout is reset by simply calling a blank page on my site. I am doing this because I have an external site hosted in my web that isn't...
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
1
by: Doogie | last post by:
Hi, I am having a problem reading a cookie that I can see is being created. I create the cookie in my global.aspx.vb page like so: Sub Session_Start(ByVal sender As Object, ByVal e As...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.