get posted form data in python

Reply

Join Date: Apr 2005
Posts: 1
Reputation: waxydock is an unknown quantity at this point 
Solved Threads: 0
waxydock waxydock is offline Offline
Newbie Poster

get posted form data in python

 
0
  #1
Apr 22nd, 2005
hi all,

i need to get all the data in the list (which is posted in the form) and store it, separating each list item with comma's. Its really clear in the code...this is what ive tried so far..it doesnt compile and it seems to complain about this line of code photos = form["photolist"]...thanks for any help

html form--->add.html
  1. <form name="listadd" method=post action=addDo.cgi>
  2. <select name="photolist" size="9" style="width: 500; height: 50"></select>
  3. <input type="submit">
  4. </form>

python cgi script--->addDo.cgi
  1. import cgitb; cgitb.enable()
  2. import cgi
  3. import sys
  4.  
  5. form = cgi.FieldStorage()
  6. photos = form["photolist"]
  7.  
  8. i=0
  9. for photo in photos:
  10. i = i + 1
  11. finalPhoto = finalPhoto + "," + photo[i]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: get posted form data in python

 
0
  #2
Apr 27th, 2005
All I could find in my Python toolbox that pertains to the retrieval of form values is this little NET module written by Mark Lee Smith. It is supposed to make CGI programming in Python easier.
Attached Files
File Type: zip net.zip (13.2 KB, 16 views)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1
Reputation: tomservo291 is an unknown quantity at this point 
Solved Threads: 0
tomservo291 tomservo291 is offline Offline
Newbie Poster

Re: get posted form data in python

 
0
  #3
Aug 15th, 2005
Here is a small module/httpwrapper that I wrote that parses GET, POST and SERVER data and stores seperate lists of keys, values and a dict of pairs of each.

  1. # Alexander Sherwin
  2. # 08/14/2005 v1.2
  3. #
  4. # This class is designed to look for and parse miscellaneous data
  5. # that is not handled internally through Python (i.e. POST, GET.)
  6. #
  7. # Currently, upon instantiation of the class it automatically parses
  8. # all POST, GET and SERVER data available to the script.
  9. #
  10. # Report any errors, bugs or contributions & additions
  11. # to alex.sherwin@gmail.com. This class has been developed and tested on
  12. # nearlyfreespeech.net servers (FreeBSD, Apache 2.x.)
  13.  
  14. import sys
  15. import os
  16.  
  17. class HTTPWrap:
  18. """Takes the sys.stdin sent from the webserver and parses it for POST variables
  19. This class may not work if your webserver sends other types of information to
  20. serverside scripts as sys.stdin
  21.  
  22. .process_post() - Initiates the parsing of raw data in std.in
  23. .process_get() - Initiates the parsing of ENV var QUERY_STRING
  24. .server(str) - Checks SERVER vars in server_dict and returns the value
  25. - if the key exists, False if it doesn't
  26.  
  27. .post_keys - List of POST keys (form names)
  28. .post_vals - List of POST values (form values)
  29. .post_dict - Dict of POST keys and values
  30. .get_keys - List of GET keys
  31. .get_vals - List of GET values
  32. .get_dict - Dict of GET keys and values
  33. .server_keys - List of SERVER keys
  34. .server_vals - List of SERVER values
  35. .server_dict - Dict of SERVER keys and values
  36.  
  37. .has_post - True if script has received POST data
  38. .has_get - True if script has received GET data
  39.  
  40. .server_dict - Dict of SERVER keys and values"""
  41.  
  42. post_keys = [] # init all lists & dictionaries
  43. post_vals = []
  44. post_dict = {}
  45. get_keys = []
  46. get_vals = []
  47. get_dict = {}
  48. server_keys = []
  49. server_vals = []
  50. server_dict = {}
  51.  
  52. has_post = False # Instantiates with no POST vars
  53. has_get = False # Instantiates with no GET vars
  54.  
  55. def __init__(self):
  56. """Initialize by determining if there is any POST or GET vars"""
  57.  
  58. self.__raw = sys.stdin.readline() # read from sys.stdin
  59. if len(self.__raw):
  60. self.process_post() # if POST string length > 0, parse it
  61.  
  62. self.__raw = "" # init empty string
  63.  
  64. for k in os.environ.items():
  65. if "QUERY_STRING" in k: # find GET data
  66. self.__raw = k[1] # save GET data
  67. else:
  68. self.server_dict[k[0]] = k[1] # save SERVER vars
  69. self.server_keys.append(k[0])
  70. self.server_vals.append(k[1])
  71.  
  72. if len(self.__raw):
  73. self.process_get() # if GET string length > 0, parse it
  74.  
  75. def server(self, serverVar):
  76. """Check server_dict for a server variable, return it's value"""
  77.  
  78. if self.server_dict.has_key(serverVar):
  79. return self.server_dict[serverVar] # return value
  80. else: return False
  81.  
  82. def process_post(self):
  83. """Read sys.stdin and parse it to obtain orderly POST data"""
  84.  
  85. self.has_post = True # set boolean for exterior uses
  86. rawlist = self.__raw.split('&') # obtain list of key=val strings
  87.  
  88. for k in rawlist:
  89. tmpli = k.split('=') # get key, val list
  90. self.post_keys.append(tmpli[0]) # save key
  91. self.post_vals.append(tmpli[1]) # save value
  92. self.post_dict[tmpli[0]] = tmpli[1] # save pair in dict
  93.  
  94. def process_get(self):
  95. """Parse self.__raw to obtain orderly GET data"""
  96.  
  97. self.has_get = True # set boolean for exterior uses
  98. rawlist = self.__raw.split('&') # obtain list of key=val strings
  99.  
  100. for k in rawlist:
  101. tmpli = k.split('=') # get key, val list
  102. self.get_keys.append(tmpli[0]) # save key
  103. self.get_vals.append(tmpli[1]) # save value
  104. self.get_dict[tmpli[0]] = tmpli[1] # save pair in dict
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