473,386 Members | 1,785 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,386 software developers and data experts.

Problem dealing with cookies

Hello,

I have the following code:

#!/usr/local/bin/python
import cgi
import Cookie

C = Cookie.SimpleCookie()
C["moo"] = "f"
C["moo"]["path"] = "/"
C["moo"]["expires"] = 60*60*60*60

#Print the headers
print C
print "Content-Type: text/html\n"

#Print starting html
print "<html><head><title>Cookie</title></head><body>"

form = cgi.FieldStorage()

if C['moo'].value != "f":
print "I remember you %s" % (C["moo"].value)

elif form.has_key("name") and C["moo"].value == "f":
print "Thank you %s!" % (form["name"].value)
C["moo"] = form['name'].value
else:
print '<form method="POST" action="ses.py">'
print '<input type="text" name="name">'
print '</form>'

print "</body></html>"

I am new in learning Python and I can't get this to work. Basically,
it asks you to input something and thanks you with the display of what
you entered.

It should remember you. SO when you refresh, it should say that it
remembers you. But it doesn't and shows the input box again.

What could possibly be wrong here?
Thanks in advance!

Fazer
Jul 18 '05 #1
1 2204
fa****@jaredweb.com (Fazer) writes:
I have the following code: [...] C = Cookie.SimpleCookie()
C["moo"] = "f"
C["moo"]["path"] = "/"
C["moo"]["expires"] = 60*60*60*60

#Print the headers
print C
Here (the print statement), you're setting the client's cookie "moo"
to "f", no matter what. What you wanted was to put the form's data in
the cookie, if the request contains form data.

print "Content-Type: text/html\n"

#Print starting html
print "<html><head><title>Cookie</title></head><body>"

form = cgi.FieldStorage()
[...]
if C['moo'].value != "f":
print "I remember you %s" % (C["moo"].value)
You're expecting C to have magically aquired a new value between the
point where you set it above and this point. How is that supposed to
happen?

elif form.has_key("name") and C["moo"].value == "f":
print "Thank you %s!" % (form["name"].value)
C["moo"] = form['name'].value

[...]

More of the same. You're setting C["moo"], but how is the client
supposed to find out? SimpleCookie isn't quite that magical. :-)

Remember the way all this gets executed: a request comes in, the web
server picks the right CGI script based on the request URL, and your
script sees the HTTP headers (including Cookie) as environment
variables. You use the headers from the environment (with something
like C.load(os.environ["HTTP_COOKIE"]) and forms = cgi.FieldStorage()
-- both look in os.environ, one explicitly, the other not), then you
output some HTTP headers and then some data (usually HTML). Request,
response. You're trying to sneak several request-response cycles into
one CGI execution.

So, you either want a single CGI script and some kind of switch (am I
looking at the first request, before the user has seen the form, or
the second, with the submitted form data?), or two CGI scripts (one
for the first request, one for the second with the form data -- set
the form's action HTML-attribute to point to that second CGI script).

Probably, you'll quickly find that life is simpler (for anything
non-trivial) if you use a web programming framework. Don't be afraid
of them! Albatross might be a good choice -- designed to be a small
step up from CGI, and pure Python so easy to install on a server --
but there are plenty of others.

In all cases, remember that you have to assume you're dealing not with
a well-behaved user (no such beast) using a well-behaved browser, but
an evil hacker crafting evil HTTP headers and data exactly how he
wants them.
John
Jul 18 '05 #2

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

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
10
by: Tony Archer | last post by:
Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a production box. On the dev box everything works fine, in production it hoses: One of...
1
by: Tony Archer | last post by:
(Forgive the dupicate post, I had left out the OS Version out of the prior post.) Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a...
0
by: Ori | last post by:
Hi, In continue to another post which I have in this news group, I was able to find out some more details about the problem What is my problem? I currently build some kind of web-service...
2
by: Netanel | last post by:
Hi, I have a site that I developed in ASP / VBScript. Some of the visitors are complaining that they can't get in into the system (using the login form of-course). My login form includes...
1
by: rushik | last post by:
Dear all, We have created a business portal for our organization. The technology used for that is LAMP. Our major access management system of the portal is based on cookies. we set some user...
0
by: Claudio | last post by:
I have a demo app where cookies does not work. The first page create a cookies. The second read the contet. If I browse the pages via IE6.0 the pages does not work. If I browse the pages via...
0
by: jegray | last post by:
I am very much a beginner in dealing with connection statments. I am getting the following error: Microsoft OLE DB Provider for ODBC Drivers error '80004005' Data source name not found and no...
7
by: cmrchs | last post by:
Hi, In Windows Vista: where does asp.net write its cookies? I use HttpCookie objCookie = new HttpCookie("nameCookie"); in Win2000 (and later) they used to be in C:\Documents and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.