Re: cgi in python

kevanh@lsl.co.uk
Thu, 29 Sep 94 11:08:05 +0100

In message <36c6j6$6uf@ikarus.rz.tu-clausthal.de> you write:
>
> I'm just beginning my experiments with python for cgi-s.
> The first goal is a mail form in python without a driving html-document.
>
> i.e.: as I don't want form definitions and the cgi program to live in
> different files (or even directories) my mailform program sends the mailform
> html doc when called without queries.
>
My python mail form program is below. It uses two modules I have written for
cgi parsing and html writing. They need a bit of tidying up but I could post
them if people are interested.

Kevan

#!/tools/net/bin/python

import sys,os,string
import html,cgi

sendmail_command = '/usr/lib/sendmail -t'

# -----------------------------------------------------------------------------
def main ():

# Read the cgi environment
#
cgi_env = cgi.CGI ()

# Output the mime header for the client
#
cgi.mime_content_header (sys.stdout,'text/html')

# Act on request type
#
if cgi_env.request_method == 'GET':

# Create an html object for writing the output
#
out = html.html_write (sys.stdout)

# Output a standard begining
#
out.std_start ('','Mail A User')

# Create the mail form
#
out.form_begin ('POST','http://www:8080/cgi/mail')

out.horizontal_rule ()
out.pre_begin ()

out.write ('From :')
out.form_text_line ('from','',60,0)
out.break_line ()

out.write ('To :')
out.form_text_line ('to','',60,0)
out.break_line ()

out.write ('Subject :')
out.form_text_line ('subject','',60,0)
out.break_line ()

out.form_text_area ('message','',15,70)

out.pre_end ()

out.form_submit ('Send Mail')
out.form_reset ('Clear')

out.horizontal_rule ()

# End the form
#
out.form_end ()

out.finish()

elif cgi_env.request_method == 'POST':

# Get the form values from the cgi environment
#
form = cgi.Form(cgi_env)

# Create sendmail message
#
sendmail_input = []

sendmail_input.append ('From: '+form.value('from')+'\n')
sendmail_input.append ('To: '+form.value('to')+'\n')
sendmail_input.append ('Subject: '+form.value('subject')+'\n')
sendmail_input.append ('\n')

sendmail_input.append (form.value('message'))
sendmail_input.append ('\n')

# Send the message using sendmail
#
pd = os.popen (sendmail_command,'w')
for line in sendmail_input:
pd.write (line)
status = pd.close ()

# Output mail sending return status
#
out = html.html_write (sys.stdout)

out.std_start ('','Mail Response')

if status == None:
out.writeline ('Message sent ok')
else:
out.writeline ('Error sending message')

out.finish ()

else:
pass

# -----------------------------------------------------------------------------
main()