I am a newbie. I wanted to know how to handle array based fields in CGI?
<form name="frmLogin" method="get" action="">
Username: <input type="text" name="login[username]" /><br/>
Password: <input type="password" name="login[password]" />&l开发者_如何学Pythont;br/>
<input type="submit" name="login[submit]" />
</form>
I have a form like above. How can I get the login field as a dictionary where keys will be username, password, submit with their corresponding values.
i can get individually by this => form["login[username]"].value but what if I dont know the key, i.e checkbox[] Do I need to process the posts and manipulate by manual coding or there is any other way to do it?
in php $_GET['login'] will give me the array of defined key value pair, I need something like that.
Thanks.
I don't think the cgi.FieldStorage
has a similar method as the one you describe in php. However, you could write something like this to accomplish the same thing, given the name[attr] format you have on your field names:
def get(form, prefix):
output = {}
def parse(key):
name = key.split('[')[1].rstrip(']')
output[name] = form[key]
map(parse, [key for key in form.keys() if key.startswith(prefix)]
return output
So, your in cgi it would look something like:
form = cgi.FieldStorage()
login_info = get(form, 'login')
print login_info['username']
print login_info['password']
精彩评论