Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
problem with simple Cookie creation....??? Posted by andohung on 5 Apr 2003 at 10:29 PM
This message was edited by andohung at 2003-4-5 22:30:30

I tried to create a Cookie in python and try to test it if its in the os.environ.
but the Cookie wasn't created or detected afterwards...
here is my code.

C = Cookie.SimpleCookie()
C["username"] = tusername
C["validated"] = tvalidated

if os.environ.has_key("HTTP_COOKIE"):
cookie_exist = "yes"
else:
cookie_exist = "no"

print cookie_exist

anyone pls help!


Report
Re: problem with simple Cookie creation....??? Posted by infidel on 7 Apr 2003 at 8:41 AM
This message was edited by Moderator at 2003-4-7 8:44:42

: This message was edited by andohung at 2003-4-5 22:30:30

: I tried to create a Cookie in python and try to test it if its in the os.environ.
: but the Cookie wasn't created or detected afterwards...
: here is my code.
:
: C = Cookie.SimpleCookie()
: C["username"] = tusername
: C["validated"] = tvalidated
:
: if os.environ.has_key("HTTP_COOKIE"):
: cookie_exist = "yes"
: else:
: cookie_exist = "no"
:
: print cookie_exist
:
: anyone pls help!

Well, what did you do with the cookie once you created it? If you don't include it in the http headers of a webpage, it will never be set anywhere. Cookies are used for state management in web browsers. You set them by including Set-Cookie directives in the http headers of a webpage, and the browser returns them when it requests that page again from the server.

Here's a simple script I just wrote and dropped in my Apache server's cgi-bin:

#!C:\Python22\python.exe

import Cookie, cgi, cgitb, os

cgitb.enable()

print 'Content-type: text/plain'

c = Cookie.SimpleCookie()
c['username'] = 'bobjones'
c['password'] = 'foobar'

print c

print # end of headers

for k, v in os.environ.items():
	print k, "=", v


The first time I viewed it, there was no HTTP_COOKIE variable in the results because the cookie wasn't set until I viewed the page. Reloading the page, however, included the HTTP_COOKIE variable because my browser was then able to return it to the server.


infidel



Report
Re: problem with simple Cookie creation....??? Posted by ErikTheGame on 7 Apr 2003 at 1:00 PM
: This message was edited by Moderator at 2003-4-7 8:44:42

: : This message was edited by andohung at 2003-4-5 22:30:30

: : I tried to create a Cookie in python and try to test it if its in the os.environ.
: : but the Cookie wasn't created or detected afterwards...
: : here is my code.
: :
: : C = Cookie.SimpleCookie()
: : C["username"] = tusername
: : C["validated"] = tvalidated
: :
: : if os.environ.has_key("HTTP_COOKIE"):
: : cookie_exist = "yes"
: : else:
: : cookie_exist = "no"
: :
: : print cookie_exist
: :
: : anyone pls help!
:
: Well, what did you do with the cookie once you created it? If you don't include it in the http headers of a webpage, it will never be set anywhere. Cookies are used for state management in web browsers. You set them by including Set-Cookie directives in the http headers of a webpage, and the browser returns them when it requests that page again from the server.
:
: Here's a simple script I just wrote and dropped in my Apache server's cgi-bin:
:
:
: #!C:\Python22\python.exe
: 
: import Cookie, cgi, cgitb, os
: 
: cgitb.enable()
: 
: print 'Content-type: text/plain'
: 
: c = Cookie.SimpleCookie()
: c['username'] = 'bobjones'
: c['password'] = 'foobar'
: 
: print c
: 
: print # end of headers
: 
: for k, v in os.environ.items():
: 	print k, "=", v
: 

:
: The first time I viewed it, there was no HTTP_COOKIE variable in the results because the cookie wasn't set until I viewed the page. Reloading the page, however, included the HTTP_COOKIE variable because my browser was then able to return it to the server.
:
:
: infidel
:
:
:
:
Can you make websites with Python?!

Id think that the regualr HTML
 Would be a lot better =P 

Report
Re: problem with simple Cookie creation....??? Posted by infidel on 7 Apr 2003 at 1:04 PM
: Can you make websites with Python?!
:
: Id think that the regualr HTML
:
 Would be a lot better =P 


Define "make websites with Python". If you mean "can you write cgi scripts in Python" then the answer is, obviously, "yes". You can also embed the Python interpreter into Apache, though I haven't been able to make that work beyond the trivial test script that comes with mod_python. You can also create high-quality, interactive, dynamic websites using Zope and/or Plone which are both written in Python. There are other programs too: Draco, CherryPy, Spyce, etc that all have something to do with "making websites with Python".


infidel

Report
Re: problem with simple Cookie creation....??? Posted by ErikTheGame on 9 Apr 2003 at 8:19 AM
: : Can you make websites with Python?!
: :
: : Id think that the regualr HTML
: :
 Would be a lot better =P 

:
: Define "make websites with Python". If you mean "can you write cgi scripts in Python" then the answer is, obviously, "yes". You can also embed the Python interpreter into Apache, though I haven't been able to make that work beyond the trivial test script that comes with mod_python. You can also create high-quality, interactive, dynamic websites using Zope and/or Plone which are both written in Python. There are other programs too: Draco, CherryPy, Spyce, etc that all have something to do with "making websites with Python".
:
:
: infidel
:
:
wow!! then thats awesome!!! I can do the easy math things
 a = 1 
b = 2
a + b = 3 


things like that =D tonight, when i get home, im at school again, im going to go through a lot of the tutorials, i tried to use Flash 5, to make a website, but it didnt work too well!!

I will also get some of the add-ons, is
Python.org the place to get the add-ons for python?

Also, a question about PH, how do i save my signature? hehe
Report
Re: problem with simple Cookie creation....??? Posted by infidel on 9 Apr 2003 at 10:43 AM
: things like that =D tonight, when i get home, im at school again, im going to go through a lot of the tutorials, i tried to use Flash 5, to make a website, but it didnt work too well!!

Well Flash is something totally different. That is for making animations and fancy dynamic stuff like java applets. Python doesn't do that. You would use python to make a site like Programmer's Heaven or Plone.org. No animations or anything, but message boards, mail, stuff like that.

: I will also get some of the add-ons, is
: Python.org the place to get the add-ons for python?

Python.org has links to many projects. Also try SourceForge.net. There's also the Vaults of Parnassus at http://www.vex.net/parnassus/

: Also, a question about PH, how do i save my signature? hehe

Click on the "My Settings" link in the blue "Members" box near the upper left. Then click on the "Message Board Settings" link near the top.


infidel

Report
Re: problem with simple Cookie creation....??? Posted by ErikTheGame on 21 Apr 2003 at 8:15 AM
: : things like that =D tonight, when i get home, im at school again, im going to go through a lot of the tutorials, i tried to use Flash 5, to make a website, but it didnt work too well!!
:
: Well Flash is something totally different. That is for making animations and fancy dynamic stuff like java applets. Python doesn't do that. You would use python to make a site like Programmer's Heaven or Plone.org. No animations or anything, but message boards, mail, stuff like that.
:
: : I will also get some of the add-ons, is
: : Python.org the place to get the add-ons for python?
:
: Python.org has links to many projects. Also try SourceForge.net. There's also the Vaults of Parnassus at http://www.vex.net/parnassus/
:
: : Also, a question about PH, how do i save my signature? hehe
:
: Click on the "My Settings" link in the blue "Members" box near the upper left. Then click on the "Message Board Settings" link near the top.
:
:
: infidel
:
:
that plone.org is pretty cool....

is iStormGames.com built the same way? thats my guild, i make some of the animations in flash for it...

i wanna be able to make a msg board, and make a login database, i will try that one day =D
k

Pe@cE

Report
Re: problem with simple Cookie creation....??? Posted by infidel on 21 Apr 2003 at 3:43 PM
: i wanna be able to make a msg board, and make a login database, i will try that one day =D

Making a message board is easy. Making a good message board takes more than just programming skills.


infidel

Report
Re: problem with simple Cookie creation....??? Posted by andohung on 8 Apr 2003 at 1:35 AM
: Well, what did you do with the cookie once you created it? If you don't include it in the http headers of a webpage, it will never be set anywhere. Cookies are used for state management in web browsers. You set them by including Set-Cookie directives in the http headers of a webpage, and the browser returns them when it requests that page again from the server.
:
: Here's a simple script I just wrote and dropped in my Apache server's cgi-bin:
:
:
: #!C:\Python22\python.exe
: 
: import Cookie, cgi, cgitb, os
: 
: cgitb.enable()
: 
: print 'Content-type: text/plain'
: 
: c = Cookie.SimpleCookie()
: c['username'] = 'bobjones'
: c['password'] = 'foobar'
: 
: print c
: 
: print # end of headers
: 
: for k, v in os.environ.items():
: 	print k, "=", v
: 

:
: The first time I viewed it, there was no HTTP_COOKIE variable in the results because the cookie wasn't set until I viewed the page. Reloading the page, however, included the HTTP_COOKIE variable because my browser was then able to return it to the server.
:
:
: infidel
:
:
:
:

what is "cgitb"
i used this script and try to run it
but it returns an import module error on the "cgitb"

and what do u mean by "include the cookie in the http headers of a webpage"????

is it just to create the cookie after printing "'Content-type: text/plain'" as the header?
Report
Re: problem with simple Cookie creation....??? Posted by infidel on 8 Apr 2003 at 10:26 AM
: :
: : #!C:\Python22\python.exe
: : 
: : import Cookie, cgi, cgitb, os
: : 
: : cgitb.enable()
: : 
: : print 'Content-type: text/plain'
: : 
: : c = Cookie.SimpleCookie()
: : c['username'] = 'bobjones'
: : c['password'] = 'foobar'
: : 
: : print c
: : 
: : print # end of headers
: : 
: : for k, v in os.environ.items():
: : 	print k, "=", v
: : 

: :
: : The first time I viewed it, there was no HTTP_COOKIE variable in the results because the cookie wasn't set until I viewed the page. Reloading the page, however, included the HTTP_COOKIE variable because my browser was then able to return it to the server.
: :
: :
: : infidel
: :
:
: what is "cgitb"
: i used this script and try to run it
: but it returns an import module error on the "cgitb"
:
: and what do u mean by "include the cookie in the http headers of a webpage"????
:
: is it just to create the cookie after printing "'Content-type: text/plain'" as the header?

cgitb is a cgi traceback module. It returns an html formatted error message to help with debugging. If it won't import then you must have an older version of Python or that module is missing. Just remove those commands from the script.

HTTP data has information called headers that comes before the actual data. The header is separated from the data by a blank line. Basically, anything that comes before a blank line is treated by a web browser as a sort of metadata that tells the browser what to do with the data that follows.

Content-type: text/html
Set-Cookie: username=john;

<html>
...
</html>



infidel

Report
Re: problem with simple Cookie creation....??? Posted by andohung on 8 Apr 2003 at 1:46 AM
Now I modified the code to see if the the Cookie C exist...

-----------------------------------------------------------
C = Cookie.SimpleCookie()
C["username"] = tusername
C["validated"] = tvalidated

print C #just to see if the Cookie has been created...

if os.environ.has_key("HTTP_COOKIE"):
cookie_exist = "yes, Cookie detected by HTTP"
else:
cookie_exist = "no, Cookie does not detected by HTTP"

print cookie_exist
----------------------------------------------------------

When I run the cgi in the browser, The result is:
Set-Cookie: username=john; Set-Cookie: validated=yes; no Cookie does not detected by HTTP

so the Cookie exist by does not store in the os.environ......
whats wrong???
Report
Re: problem with simple Cookie creation....??? Posted by infidel on 8 Apr 2003 at 10:20 AM
: Now I modified the code to see if the the Cookie C exist...
:
: -----------------------------------------------------------
: C = Cookie.SimpleCookie()
: C["username"] = tusername
: C["validated"] = tvalidated
:
: print C #just to see if the Cookie has been created...
:
: if os.environ.has_key("HTTP_COOKIE"):
: cookie_exist = "yes, Cookie detected by HTTP"
: else:
: cookie_exist = "no, Cookie does not detected by HTTP"
:
: print cookie_exist
: ----------------------------------------------------------
:
: When I run the cgi in the browser, The result is:
: Set-Cookie: username=john; Set-Cookie: validated=yes; no Cookie does not detected by HTTP
:
: so the Cookie exist by does not store in the os.environ......
: whats wrong???

There's nothing wrong. Even though you've created a cookie object, it doesn't get set in the environment until it has been set in the browser so the browser can include it as part of the request. Think of cookies as variables in the browser. When the browser calls your script, the variables are not set yet. Your script creates the variables and sends them back to the browser as part of the http text. Now they are set in the browser. The NEXT time that browser requests your script, it sends those variables back to the server as part of the request. Only then will os.environ have an HTTP_COOKIE variable set. See, os.environ is the environment of the server. You have created a python object that wraps cookies, but you are not modifying the server environment. You're just managing the values that you will send to the client.

1. Browser requests script from server

2. Server executes script and returns data, including cookie values, to browser

3. Browser loads html and sets cookies

4. Browser requests same script from server again and sends the cookie values

5. Server accepts request and sets HTTP_COOKIE to contain cookie values sent by browser.


infidel

Report
Re: problem with simple Cookie creation....??? Posted by nevle on 2 Sept 2003 at 6:14 PM
This message was edited by Moderator at 2003-9-3 8:12:54

hi guys,
I have read all the posting on this but am also having trouble with cookie reading (or setting??)...
This doesn't run, could someone explain to me why?
#! /usr/bin/python
import cgi
from Cookie import SimpleCookie

html="""
<html><h3>test page </h3></html>"""
c = SimpleCookie()
c["username"] = "me"

if os.environ.has_key("HTTP_COOKIE"):
    cookie_exist = "yes, Cookie detected by HTTP"
else:
    cookie_exist = "no, Cookie does not detected by HTTP"
    
print c
print "Content-type: text/html\n\n"
print c
print html

Thank you for your help,
nevle

the board doesn't understand html tags, you have to use square brackets

Report
Re: problem with simple Cookie creation....??? Posted by infidel on 3 Sept 2003 at 8:16 AM
: This message was edited by Moderator at 2003-9-3 8:12:54

: hi guys,
: I have read all the posting on this but am also having trouble with cookie reading (or setting??)...
: This doesn't run, could someone explain to me why?
:
: #! /usr/bin/python
: import cgi
: from Cookie import SimpleCookie
: 
: html="""
: <html><h3>test page </h3></html>"""
: c = SimpleCookie()
: c["username"] = "me"
: 
: if os.environ.has_key("HTTP_COOKIE"):
:     cookie_exist = "yes, Cookie detected by HTTP"
: else:
:     cookie_exist = "no, Cookie does not detected by HTTP"
:     
: print c
: print "Content-type: text/html\n\n"
: print c
: print html
: 

: Thank you for your help,
: nevle
:
: the board doesn't understand html tags, you have to use square brackets

You didn't import "os".

Try also adding this line to the top, it may help in debugging cgi scripts running in a webserver:

import cgitb; cgitb.enable()

Look up the cgi and cgitb modules in your documentation for advice about their use.


infidel




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.