944,111 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 23402
  • Python RSS
Apr 22nd, 2005
0

get posted form data in python

Expand Post »
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
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
waxydock is offline Offline
1 posts
since Apr 2005
Apr 27th, 2005
0

Re: get posted form data in python

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, 154 views)
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 15th, 2005
0

Re: get posted form data in python

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.

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomservo291 is offline Offline
1 posts
since Aug 2005
Jun 16th, 2010
0
Re: get posted form data in python
I am about as new as you can get to form retrieval and pass the variables to my python code. I'm sure that these scripts can help but is there an example that I can look at where these scripts are implemented.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bikehike90 is offline Offline
9 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: List all instances of a class
Next Thread in Python Forum Timeline: Please help!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC