| | |
get posted form data in python
![]() |
•
•
Join Date: Apr 2005
Posts: 1
Reputation:
Solved Threads: 0
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 cgi script--->addDo.cgi
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)
<form name="listadd" method=post action=addDo.cgi> <select name="photolist" size="9" style="width: 500; height: 50"></select> <input type="submit"> </form>
python cgi script--->addDo.cgi
Python Syntax (Toggle Plain Text)
import cgitb; cgitb.enable() import cgi import sys form = cgi.FieldStorage() photos = form["photolist"] i=0 for photo in photos: i = i + 1 finalPhoto = finalPhoto + "," + photo[i]
•
•
Join Date: Aug 2005
Posts: 1
Reputation:
Solved Threads: 0
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)
# Alexander Sherwin # 08/14/2005 v1.2 # # This class is designed to look for and parse miscellaneous data # that is not handled internally through Python (i.e. POST, GET.) # # Currently, upon instantiation of the class it automatically parses # all POST, GET and SERVER data available to the script. # # Report any errors, bugs or contributions & additions # to alex.sherwin@gmail.com. This class has been developed and tested on # nearlyfreespeech.net servers (FreeBSD, Apache 2.x.) import sys import os class HTTPWrap: """Takes the sys.stdin sent from the webserver and parses it for POST variables This class may not work if your webserver sends other types of information to serverside scripts as sys.stdin .process_post() - Initiates the parsing of raw data in std.in .process_get() - Initiates the parsing of ENV var QUERY_STRING .server(str) - Checks SERVER vars in server_dict and returns the value - if the key exists, False if it doesn't .post_keys - List of POST keys (form names) .post_vals - List of POST values (form values) .post_dict - Dict of POST keys and values .get_keys - List of GET keys .get_vals - List of GET values .get_dict - Dict of GET keys and values .server_keys - List of SERVER keys .server_vals - List of SERVER values .server_dict - Dict of SERVER keys and values .has_post - True if script has received POST data .has_get - True if script has received GET data .server_dict - Dict of SERVER keys and values""" post_keys = [] # init all lists & dictionaries post_vals = [] post_dict = {} get_keys = [] get_vals = [] get_dict = {} server_keys = [] server_vals = [] server_dict = {} has_post = False # Instantiates with no POST vars has_get = False # Instantiates with no GET vars def __init__(self): """Initialize by determining if there is any POST or GET vars""" self.__raw = sys.stdin.readline() # read from sys.stdin if len(self.__raw): self.process_post() # if POST string length > 0, parse it self.__raw = "" # init empty string for k in os.environ.items(): if "QUERY_STRING" in k: # find GET data self.__raw = k[1] # save GET data else: self.server_dict[k[0]] = k[1] # save SERVER vars self.server_keys.append(k[0]) self.server_vals.append(k[1]) if len(self.__raw): self.process_get() # if GET string length > 0, parse it def server(self, serverVar): """Check server_dict for a server variable, return it's value""" if self.server_dict.has_key(serverVar): return self.server_dict[serverVar] # return value else: return False def process_post(self): """Read sys.stdin and parse it to obtain orderly POST data""" self.has_post = True # set boolean for exterior uses rawlist = self.__raw.split('&') # obtain list of key=val strings for k in rawlist: tmpli = k.split('=') # get key, val list self.post_keys.append(tmpli[0]) # save key self.post_vals.append(tmpli[1]) # save value self.post_dict[tmpli[0]] = tmpli[1] # save pair in dict def process_get(self): """Parse self.__raw to obtain orderly GET data""" self.has_get = True # set boolean for exterior uses rawlist = self.__raw.split('&') # obtain list of key=val strings for k in rawlist: tmpli = k.split('=') # get key, val list self.get_keys.append(tmpli[0]) # save key self.get_vals.append(tmpli[1]) # save value self.get_dict[tmpli[0]] = tmpli[1] # save pair in dict
![]() |
Similar Threads
- Form data & Back button (PHP)
- User Form error checking (PHP)
- internet form handling (Python)
- Posting form data to an Oracle database (JavaScript / DHTML / AJAX)
Other Threads in the Python Forum
- Previous Thread: more information about python
- Next Thread: Musical Beeps (Python)
| Thread Tools | Search this Thread |
address alarm anydbm app beginner changecolor cipher clear conversion coordinates corners curves cx-freeze data definedlines development dictionary directory dynamic events excel feet file float format function generator getvalue halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse number numbers output parsing path port prime programming py2exe pygame pymailer python queue random rational raw_input recursion recursive screensaverloopinactive script scrolledtext searchingfile singleton slicenotation split ssh string strings tails text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice word wxpython xlwt






