faqts : Computers : Programming : Languages : Python : Snippets : Web Programming / Manipulating HTML files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 10 people (60%) answered Yes
Recently 4 of 8 people (50%) answered Yes

Entry

Taking input from HTML form (CGI)

Jul 5th, 2000 10:00
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 123, Paul Winkler


"""
Packages: networking.internet.cgi
"""
"""
I wanted to do a very basic CGI script that takes input from an html
form and sends a message. A bit of deja-newsing turned up several
suggestions in comp.lang.python. I hacked a couple of them together and
came up with this:
"""
#!/usr/bin/python
import cgi, os
print "content-type: text/html"
print
print"<HEAD><TITLE>Message sent.</TITLE></HEAD>"
results=cgi.FieldStorage() #Always do this ONCE to get input from form.
address = results["address"].value
return_address=results["return_addr"].value
subject = results["subject"].value
text = results["text"].value
print"<p>Your message has been sent to", address
# Use sendmail to do the actual sending.
mailpipe = "/usr/lib/sendmail " + address
#  os.popen means "pipe open". So we are
#  opening a pipe to sendmail, then printing to it.
m = os.popen(mailpipe, "w") 
m.write("To: " + address + "\n")
m.write("From: " + return_address + "\n")
m.write("Subject: " + subject + "\n\n")
m.write(text)
m.close()
# End of script