How do I get the value of an HTML text field in Python?

advertisements

I have a Python code, and when I enter 127.0.0.1\dateform as an example it shows a simple HTML page which is created by Python, and it has just a textbox, and a submit button. Now I wanted get the value of the textbox. I almost tried everything that I could but I couldn't find anything. By the way I'm very new in Python.

Please let me know how it is possible?

Thanks.


Old good cgi :)

I hope you've already set your cgi enviroment. Useful resource. Another useful resource

Let's say you have a form

form.html

<form action="/cgi-bin/form.py" method="get" enctype="multipart/form-data">

Text input: <input maxlength="60" size="60" value="0" name="textinput"> 

<input value="Submit" type="submit">

</form>

form.py (inside cgi-bin folder)

import cgi   

form = cgi.FieldStorage()
# html form inputs are simple key value pairs -
#in php they are associative arrays
#in ruby they are hashes
# and in python dictionaries
# keys are form elements' name (in our example 'textinput')
# values are user inputs
input_text = form.getfirst("textinput", "0") # 0 is optional - if no
#values entered automatically set it to "0". same as
#>>> x = {2:"a"}
#>>> x.get(2, "nonexistent")
#'a'
#>>> x.get(3, "nonexistent")
#'nonexistent'
#have a look up this -http://www.tutorialspoint.com/python/dictionary_get.htm
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<body>"
#print "<p>%s</p>" % form  #uncomment this line if you want
#to see what does form hashes look like
print "<p>%s</p>" % input_text
print "</body>"
print "</html>"