Reading html form data from Python and e-mailing it

2 views
Skip to first unread message

Chavoux Luyt

unread,
Nov 23, 2009, 4:36:02 PM11/23/09
to ct...@googlegroups.com
Hi all!

I am pretty sure this should be possible with python without too much
trouble? From my google search it seems that I can import smtplib for
sending the e-mail. But is it possible to get the submitted content of
a web form in a static html file? Most of the search results I found
seems to suggest something like django or using dynamic web pages. I
was thinking of just putting the python file in cgi-bin and linking to
it from the submit button. This would be the only page on the site I
will use python for... all the rest will be static html (with css)
that the client can easily change and update as they want. (My other
option is finding a windows host and using asp .NET with c# (in which
I know how to do this), but that feels like total overkill for a
single web form.

Any pointers appreciated
Chavoux

Simon Cross

unread,
Nov 23, 2009, 4:52:39 PM11/23/09
to ct...@googlegroups.com
On Mon, Nov 23, 2009 at 11:36 PM, Chavoux Luyt <cha...@gmail.com> wrote:
> Any pointers appreciated

http://docs.python.org/library/cgi.html

Chavoux Luyt

unread,
Nov 23, 2009, 5:20:48 PM11/23/09
to ct...@googlegroups.com
Thanks Simon! Exactly what I was looking for!

Now for my first "Hello world" program in Python! :-)

Chavoux

2009/11/23 Simon Cross <hodg...@gmail.com>:
> On Mon, Nov 23, 2009 at 11:36 PM, Chavoux Luyt <cha...@gmail.com> wrote:
>> Any pointers appreciated
>
> http://docs.python.org/library/cgi.html
>
> --
>
> You received this message because you are subscribed to the Google Groups "Cape Town Python Users Group" group.
> To post to this group, send email to ct...@googlegroups.com.
> To unsubscribe from this group, send email to ctpug+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ctpug?hl=.
>
>
>

Chavoux Luyt

unread,
Nov 23, 2009, 5:24:44 PM11/23/09
to ct...@googlegroups.com
Thanks Simon!

2009/11/23 Simon Cross <hodg...@gmail.com>:
>
> http://docs.python.org/library/cgi.html

Stéfan van der Walt

unread,
Nov 23, 2009, 5:57:54 PM11/23/09
to ct...@googlegroups.com
Hi Chavoux

2009/11/23 Chavoux Luyt <cha...@gmail.com>:
web.py should do the trick:

http://webpy.org/
http://webpy.org/cookbok/postbasic

Regards
Stéfan

Raoul Snyman

unread,
Nov 24, 2009, 11:05:30 AM11/24/09
to ct...@googlegroups.com
2009/11/24 Stéfan van der Walt <sjvd...@gmail.com>:
> web.py should do the trick:
>
> http://webpy.org/
> http://webpy.org/cookbok/postbasic

You can also look at Pylons[1], TurboGears[2] or Django[3].


[1] http://pylonshq.com/
[2] http://turbogears.org/
[3] http://djangoproject.com/

--
Raoul Snyman
B.Tech Information Technology (Software Engineering)
E-Mail: raoul....@gmail.com
Web: http://www.saturnlaboratories.co.za/
Blog: http://blog.saturnlaboratories.co.za/
Mobile: 082 550 3754
Registered Linux User #333298 (http://counter.li.org)

Nicol van der Merwe

unread,
Nov 24, 2009, 2:30:09 PM11/24/09
to ct...@googlegroups.com
+1 for Pylons. Also see web2py (www.web2py.com).


--
John Cleese has the same initials as our Lord an Saviour, Jackie Chan.

Chavoux Luyt

unread,
Dec 7, 2009, 10:30:56 AM12/7/09
to ct...@googlegroups.com
Hi guys!

Thanks for all the help. I decided to go with the plain cgi module,
but now I am running into a problem. I am not sure if the problem is
in my html code or in the Apache setup. When I click on the button in
the html page, instead of running the Python script, it just returns
the python source code (asking if it must save or open the file). I am
fairly certain that the problem is not in the Python code itself
because it runs fine from the command-line and the file is executable.

I have Apache2 installed (Apache/2.2.9 (Debian) mod_ssl/2.2.9
OpenSSL/0.9.8g mod_jk/1.2.26 PHP/5.2.6-1+lenny3 with Suhosin-Patch
mod_wsgi/2.5 Python/2.5.2 Server at 127.0.0.1 Port 80); added to the
/etc/apache2/httpd.conf file the following line:
ScriptAlias /cgi-bin/ /var/wwww/cumeta/cgi-bin/
Running Python2.5 on Simply Mepis 8.
My html file is pretty simple:
"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Talk back</title>
</head>
<body>
<form action="./cgi-bin/backchat.py" method="post">
<button name="SubmitBtn" type="submit">Submit</button>
</form>
</body>
</html>
"
What am I missing? Why doesn't it actually run the python script?
(Also pretty straight-forward):
"
#!/usr/bin/env python

import smtplib
import cgi

print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"
"

I googled the problem and the best answer I found so far was
"http://httpd.apache.org/docs/2.0/howto/cgi.html#configuring"

Any ideas?
Chavoux

Simon Cross

unread,
Dec 7, 2009, 11:28:27 AM12/7/09
to ct...@googlegroups.com
On Mon, Dec 7, 2009 at 5:30 PM, Chavoux Luyt <cha...@gmail.com> wrote:
> I googled the problem and the best answer I found so far was
> "http://httpd.apache.org/docs/2.0/howto/cgi.html#configuring"

If you accessed the page before setting everything up correctly, try
flushing all of your browser caches and then returning to the page.
Some browser (e.g. Firefox) cache the content type information instead
of re-reading it from the server each time even if the page is
refreshed [1].

[1] On the face of it this behaviour seems rather counter-intuitive,
but perhaps the browser writers have their reasons.

Schiavo
Simon
Reply all
Reply to author
Forward
0 new messages