problem with simple Cookie creation....???

[b][red]This message was edited by andohung at 2003-4-5 22:30:30[/red][/b][hr]
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!


Comments

  • [b][red]This message was edited by Moderator at 2003-4-7 8:44:42[/red][/b][hr]
    : [b][red]This message was edited by andohung at 2003-4-5 22:30:30[/red][/b][hr]
    : 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:

    [code]
    #!C:Python22python.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
    [/code]

    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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]



  • : [b][red]This message was edited by Moderator at 2003-4-7 8:44:42[/red][/b][hr]
    : : [b][red]This message was edited by andohung at 2003-4-5 22:30:30[/red][/b][hr]
    : : 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:
    :
    : [code]
    : #!C:Python22python.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
    : [/code]
    :
    : 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.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :
    :
    :
    Can you make websites with Python?!

    Id think that the regualr HTML
    [code] Would be a lot better =P [/code]
  • : Can you make websites with Python?!
    :
    : Id think that the regualr HTML
    : [code] Would be a lot better =P [/code]

    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".


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • : 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:
    :
    : [code]
    : #!C:Python22python.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
    : [/code]
    :
    : 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.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :
    :
    :

    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?
  • 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???
  • : 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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • : : [code]
    : : #!C:Python22python.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
    : : [/code]
    : :
    : : 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.
    : :
    : :
    : : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    : :
    :
    : 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.

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


    ...

    [/code]


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • : : Can you make websites with Python?!
    : :
    : : Id think that the regualr HTML
    : : [code] Would be a lot better =P [/code]
    :
    : 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".
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :
    wow!! then thats awesome!!! I can do the easy math things
    [code] a = 1
    b = 2
    a + b = 3 [/code]

    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
  • : 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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • : : 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.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :
    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
    [size=5][red] [/red][purple] [/purple][green] [/green][blue] k [/blue][/size]

    [size=1][red]Pe[/red][green]@[/green][blue]cE[/blue][/size]

  • : 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 [italic]good[/italic] message board takes more than just programming skills.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • [b][red]This message was edited by Moderator at 2003-9-3 8:12:54[/red][/b][hr]
    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?
    [code]
    #! /usr/bin/python
    import cgi
    from Cookie import SimpleCookie

    html="""

    test page

    """
    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

    "
    print c
    print html
    [/code]
    Thank you for your help,
    nevle

    [red]the board doesn't understand html tags, you have to use square brackets[/red]

  • : [b][red]This message was edited by Moderator at 2003-9-3 8:12:54[/red][/b][hr]
    : 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?
    : [code]
    : #! /usr/bin/python
    : import cgi
    : from Cookie import SimpleCookie
    :
    : html="""
    :

    test page

    """
    : 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

    "
    : print c
    : print html
    : [/code]
    : Thank you for your help,
    : nevle
    :
    : [red]the board doesn't understand html tags, you have to use square brackets[/red]

    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.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories