Gabriel Genellina wrote:
En Mon, 05 May 2008 15:56:26 -0300, Ethan Furman <[EMAIL PROTECTED]> escribió:
I tried adding a form to our website for uploading large files.
Personally, I dislike the forms that tell you you did something wrong
and make you re-enter *all* your data again, so this one cycles and
remembers your answers, and only prompts for the file once the rest of
the entered data is good. However, the first time the form loads it can
take up to 30 seconds... any ideas why?
Hard to tell without looking at the code... And what has cgitb to do with this?
Hmmmm... excellent question, and the answer is -- Nothing. My
apologies. I'm importing cgi (cgitb was while I was debugging it), as
well as a bunch of others.
Here's the source, with a bunch of the actual html stripped out. The -u
as well as the last two lines were an attempt to eliminate the 30-second
pause while it loads, as it seems to get all data transferred, then just
waits for a while. Any ideas appreciated. My apologies for the ugly code.
--
Ethan
<code>
#!/usr/local/bin/python -u
import cgi
#import cgitb; cgitb.enable()
import smtplib
import mimetypes
import os
import sys
from email.Encoders import encode_base64
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
HEADER = """Content-type: text/html\n
<snip>
"""
SELECTFILES = """
<snip>
"""
FILESRECEIVED = """
<snip>
"""
VERIFIED = """
<snip>
"""
FOOTER = """
<snip>
</body>
</html>"""
REQUIRED = '<br><i>*required</i>'
form = cgi.FieldStorage()
clientname = clientemail = clientcsr = subject = body = ""
csrs = {"frank":{'html':'selected="yes"', 'name':'<name removed>',
'email':'<email removed>'}, \
"kathy":{'html':'', 'name':'<name removed>', 'email':'<email
removed>'}, \
"chris":{'html':'', 'name':'<name removed>', 'email':'<email
removed>'}, \
"tracy":{'html':'', 'name':'<name removed>', 'email':'<email
removed>'}, \
"susi": {'html':'', 'name':'<name removed>', 'email':'<email
removed>'}, \
"bill": {'html':'', 'name':'<name removed>', 'email':'<email
removed>'}}
incomplete = False
files = []
filelist = []
if form:
if form.has_key("name"):
clientname = form["name"].value
if form.has_key("email"):
clientemail = form["email"].value
if form.has_key("CSR"):
clientcsr = form["CSR"].value
csrs["frank"]['html'] = '' # clear
default
csrs[clientcsr]['html'] = 'selected="yes"'
if form.has_key("subject"):
subject = form["subject"].value
if form.has_key("body"):
body = form["body"].value
incomplete = not (clientname and clientemail and clientcsr and subject)
if form.has_key("file1"):
file1 = form["file1"]
if file1.filename:
files.append(file1.filename + "<br>")
filelist.append(file1)
if form.has_key("file2"):
file2 = form["file2"]
if file2.filename:
files.append(file2.filename + "<br>")
filelist.append(file2)
if form.has_key("file3"):
file3 = form["file3"]
if file3.filename:
files.append(file3.filename + "<br>")
filelist.append(file3)
if form.has_key("file4"):
file4 = form["file4"]
if file4.filename:
files.append(file4.filename + "<br>")
filelist.append(file4)
if form.has_key("file5"):
file5 = form["file5"]
if file5.filename:
files.append(file5.filename + "<br>")
filelist.append(file5)
def csrform():
cn_present = ''
ce_present = ''
sb_present = ''
if incomplete:
if not clientname:
cn_present = REQUIRED
if not clientemail:
ce_present = REQUIRED
if not subject:
sb_present = REQUIRED
print """
<html snipped>
<form action="/cgi-bin/contact_files.py"
method="POST" enctype="multipart/form-data">
<p>
Please fill in your name, e-mail address,
subject, and select your CSR. You will then be able to upload files
on the next screen. Any additional notes may go
in the Message field.
<br /></p>
<table border="0">
"""
print """
<tr><td>Your name</td><td><input type="text"
size="40" maxlength="50" name="name" value="%s">%s</td></tr>""" \
% (clientname, cn_present)
print """
<tr><td>Your e-mail</td><td><input type="text"
size="40" maxlength="100" name="email" value="%s">%s</td></tr>""" \
% (clientemail, ce_present)
print """
<tr><td>Subject line</td><td><input type="text"
size="40" maxlength="100" name="subject" value="%s">%s</td></tr>""" \
% (subject, sb_present)
print """
<tr><td>Your CSR</td><td><select name="CSR">
<option %s value="frank">Frank</option>
<option %s value="kathy">Kathy</option>
<option %s value="chris">Chris</option>
<option %s value="tracy">Tracy</option>
<option %s value="susi">Susi</option>
<option %s value="bill">Bill</option>
</select></td></tr>""" \
% (csrs["frank"]['html'],
csrs["kathy"]['html'], csrs["chris"]['html'], csrs["tracy"]['html'],
csrs["susi"]['html'], csrs["bill"]['html'])
print """
<html snipped>""" % (body)
def uploadform():
try:
msg = MIMEMultipart()
msg['To'] = "%s <%s>" % (csrs[clientcsr]['name'],
csrs[clientcsr]['email'])
msg['From'] = "Ad-Mail Upload Manager for %s
<[EMAIL PROTECTED]>" % (clientname, )
msg['Reply-to'] = clientemail
msg['Subject'] = subject
msg.attach(MIMEText(body))
for file in filelist:
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(file.file.read())
encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment',
filename=os.path.split(file.filename)[1])
msg.attach(attachment)
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail('Ad-Mail Upload Manager <[EMAIL PROTECTED]>',
csrs[clientcsr]['email'], msg.as_string())
smtp.close
except:
print "<H1>Unknown error. Please try again later.</H1>"
smtp = smtp
if not (form and clientname and clientemail and clientcsr):
print HEADER
csrform()
print FOOTER
elif files:
print HEADER
print FILESRECEIVED % (' '.join(files), csrs[clientcsr]['name'])
uploadform()
print FOOTER
else:
print HEADER
print SELECTFILES % (clientname, clientemail, subject, clientcsr,
body, clientname.split()[0], csrs[clientcsr]['name'].split()[0])
print FOOTER
sys.stdout.flush()
sys.stdout.close()
</code>
--
http://mail.python.org/mailman/listinfo/python-list