473,597 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Nam e:</b></td><td><input type=text name=\"name\"></td>
</tr>"
print "</table>"
print "<p>"
print "<table>"
print "<tr>"
print "<td><b>Rac e:</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=\"getclas s\">"
print "<input type = submit value =\"Enter\">"
def pick_class():
mycookie = Cookie.SmartCoo kie(os.environ. get("HTTP_COOKI E", ""))
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.FieldStorag e()
mycookie = Cookie.SmartCoo kie()
if form.has_key("n ame") and form.has_key("r ace"):
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 1093

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

Similar topics

7
4316
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, Nirmal
2
5558
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 want to override the last value set in the onunload event, it has no effect. The next onload still has the old value. Any ideas? The following code simulates the problem I experience. --------------------------------------------------
3
11108
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 behind and don't use a domain then I can not change or remove that cookie's value on the client. If I subsequently create the cookie again in the codebehind then I actually end up with TWO cookies with the same name in the response. The cookie...
4
1654
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 getting the one cookie? Thanks, - Stew
9
5503
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 http://www.developersdex.com ***
2
2699
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"/> </forms> </authentication> </system.web>
3
2476
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 sharing the auth mechanism. I can test this easily by simply having a hidden iframe and use a javascript call to refresh my sites blank page every time the external site loads a page and this does work fine, the cookie is refreshed as expected if...
29
2992
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 switch/case to take action based on that value in the cookie. I want to set the cookie manually, so I don't need setcookie(). I see that in PHP it stores the cookie in memory. Is there a way to have PHP just read a cookie from the desktop machine?...
1
1166
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 EventArgs) Dim user As String = Current.User.Identity.Name Dim roleCookieName As String = user & "-Roles" Dim rolesList As String = Response.Cookies(roleCookieName ).Value 'Only try to create the cookie if it is not already created.
0
7962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8024
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6681
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1229
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.