[Tutor] Cookie help

Kent Johnson kent37 at tds.net
Sun Sep 17 23:41:15 CEST 2006


federico ramirez wrote:
> Hey, im working on a cgi script, just a simple news system.
> And im stucked with the login system.
> GOD! I hate being a noob!
> Well, this is the login page
> 
> ################################
> #!/usr/bin/python
> 
> import cgi, dbm, string, Cookie
> import config
> request = cgi.FieldStorage()
> 
> def loginform():
>     print '''<form action="admin.py" method="post">
>     <table border="0">
>     <tr>
>     <td>Username</td>
>     <td><input name="user" type="text" id="user" /></td>
>     </tr>
>     <tr>
>     <td>Password</td>
>     <td><input name="pass" type="text" id="pass" /></td>
>     </tr>
>     <tr>
>     <td>&nbsp;</td>
>     <td><input type="submit" name="Submit" value="Login" /></td>
>     </tr>
>     </table>
>     </form>'''
> 
> def main():
>     C = Cookie.SimpleCookie()

This makes a new cookie but it doesn't have any data in it - you have to 
get the data from the environment and put it in the cookie. Try this:
     if os.environ.has_key('HTTP_COOKIE'):
          thiscookie.load(os.environ['HTTP_COOKIE'])

>     if(C.has_key("admin_user") and C.has_key("admin_pass")):
>         admin = config.getadmin()
>         if(C["admin_user"].value == admin[0] and C["admin_pass"].value 
> == admin[1]):
>             config.makepage("You are already logged!")
>         else:
>             config.makepage("Wrong cookies...")
>     elif(request.has_key("Submit")):
>         admin = config.getadmin()
>         username = config.clean(request["user"].value)
>         password = config.clean(request["pass"].value)
>         if(username == admin[0] and password == admin[1]):
>                 C["admin_user"] = username
>                 C["admin_pass"] = password
>                 print C
>                 print "Content-Type: text/html\n\n"
>                 config.startpage()
>                 print "Bienvenido",username,'!'
>                 config.endpage ()
>         else:
>             print "Content-Type: text/html\n\n"
>             config.startpage()
>             print "Incorrect username and password combination"
>             config.endpage ()
>     else:
>         print "Content-Type: text/html\n\n"
>         config.startpage()
>         loginform()
>         config.endpage()
> 
> main()
> ################################
> 
> That seems to work..but i cant get the cookies, i tried making this to 
> get them but i have a 500 error -.- i hate it so much
> 
> #################################
> #!/usr/bin/python
> try:
>     import Cookie
>     print "Content-Type: text/html\n\n"
>     C = Cookie.SimpleCookie()
>     print C.["admin_user"].value

Most likely you get an error here; actually a SyntaxError because of the 
extra . after C.

If you show us the tracebacks we can give more specific help.

> except:
>     cgi.print_exception()
> ###################################
> 
> i tried that in different ways but i cant get it to work
> 
> Help please :(
> -- 
> Best Regards.
> fedekiller
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list