Cookie Error

Reply

Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster

Cookie Error

 
0
  #1
Jun 27th, 2009
I have the following files:
  1. # add.py
  2. #!/usr/bin/env python
  3. import os
  4. import Cookie
  5. import hashlib
  6. import MySQLdb
  7. import cgi
  8. import cgitb; cgitb.enable()
  9. def showform():
  10. reshtml = '''Content-Type: text/html\n
  11. <html>
  12. <body>
  13. <form action='add.py' method='post'>
  14. Name: <input type=text name=name size = 15><br>
  15. URL : <input type=text name=url size=30><br>
  16. <input type=submit value="Add"></form>
  17. </body>
  18. </html>
  19. '''
  20. print reshtml
  21.  
  22. def main():
  23. conn = MySQLdb.connect(host="localhost", user="root", passwd="", db="test")
  24. reshtml = '''Content-Type: text/html\n
  25. <html><body>
  26. %s
  27. </body>
  28. </html>
  29. '''
  30. cursor = conn.cursor ()
  31. form = cgi.FieldStorage()
  32. name = form['name'].value
  33. url = form['url'].value
  34. cookie = Cookie.SimpleCookie()
  35. string_cookie = os.environ.get('HTTP_COOKIE')
  36. if string_cookie:
  37. user = cookie['u'].value
  38. else:
  39. user = "dad"
  40. cursor.execute("insert into bookmarks values ('%s', '%s', '%s')" % (user, name, url))
  41. print reshtml % ("")
  42.  
  43. form = cgi.FieldStorage()
  44. if not form.has_key('url'):
  45. showform()
  46. else:
  47. main()

  1. #!/usr/bin/env python
  2. import os
  3. import Cookie
  4. import hashlib
  5. import MySQLdb
  6. import cgi
  7. import cgitb; cgitb.enable()
  8. def showform():
  9. reshtml = '''Content-Type: text/html\n
  10. <html>
  11. <body>
  12. <a href="register.py">Not a member? </a>
  13. <form action='login.py' method="post">
  14. Username: <input type=text name=person value="New User" size = 15><br>
  15. Password: <input type=password name=pass value="" size=15><br>
  16. <input type=submit value="Login"></form>
  17. </body>
  18. </html>
  19. '''
  20. print reshtml
  21.  
  22. def main():
  23. conn = MySQLdb.connect(host="localhost", user="root", passwd="", db="test")
  24. reshtml = '''Content-Type: text/html\n
  25. <html><body>
  26. %s
  27. </body>
  28. </html>
  29. '''
  30. cursor = conn.cursor ()
  31. form = cgi.FieldStorage()
  32. who = form['person'].value
  33. passw = form['pass'].value
  34. m = hashlib.md5()
  35. m.update(passw)
  36. cursor.execute("select * from pass where user='%s' and passwrd='%s'" % (who, m.digest()))
  37. if cursor.rowcount != 1:
  38. print reshtml % ("Invalid username or password")
  39. else:
  40. cookie = Cookie.SimpleCookie()
  41. string_cookie = os.environ.get('HTTP_COOKIE')
  42. if not string_cookie:
  43. cookie['u'] = who
  44. else:
  45. cookie['u'] = who
  46. print cookie
  47. cursor.execute("select name, url from bookmarks where user='%s'" % (who))
  48. ro = cursor.fetchall()
  49. st = []
  50. for row in ro:
  51. st.append([row[0], row[1]])
  52. blah = "<a href='add.py'>add bookmark</a>\n"
  53. for s in st:
  54. for a in s:
  55. blah += a + "\t\t"
  56. blah += "\n"
  57. print reshtml % (blah)
  58.  
  59. form = cgi.FieldStorage()
  60. if not form.has_key('person'):
  61. showform()
  62. else:
  63. main()
in add.py it gives a key error at the cookie bit, why?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Cookie Error

 
0
  #2
Jun 28th, 2009
You'll need to be more clear. Define "the cookie bit". How about pasting your traceback.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC