[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

def editpage(kind, headers={}, text=''):


on Send, View+select+Reply, View+select+Fwd


pageheader(kind=kind)
print('

<form enctype="multipart/form-data" method=post', end=' ')
print('action="%sonEditPageSend.py">' % urlroot)
if mailconfig.mysignature:
text = '\n%s\n%s' % (mailconfig.mysignature, text)
messagearea(headers, text)
sendattachmentwidgets()
print('')
print('')
print('')
pagefooter()


def errorpage(message, stacktrace=True):
pageheader(kind='Error') # was sys.exc_type/exc_value
exc_type, exc_value, exc_tb = sys.exc_info()
print('

Error Description

', message)
print('

Python Exception

', cgi.escape(str(exc_type)))
print('

Exception details

', cgi.escape(str(exc_value)))
if stacktrace:
print('

Exception traceback

')
import traceback
traceback.print_tb(exc_tb, None, sys.stdout)
print('
')
pagefooter()


def confirmationpage(kind):
pageheader(kind='Confirmation')
print('

%s operation was successful

' % kind)
print('

Press the link below to return to the main page.

')
pagefooter()


def getfield(form, field, default=''):


emulate dictionary get method


return (field in form and form[field].value) or default


def getstandardpopfields(form):
"""
fields can arrive missing or '' or with a real value
hardcoded in a URL; default to mailconfig settings
"""
return (getfield(form, 'user', mailconfig.popusername),
getfield(form, 'pswd', '?'),
getfield(form, 'site', mailconfig.popservername))


def getstandardsmtpfields(form):
return getfield(form, 'site', mailconfig.smtpservername)


def runsilent(func, args):
"""
run a function without writing stdout
ex: suppress print's in imported tools
else they go to the client/browser
"""


1290 | Chapter 16: The PyMailCGI Server

Free download pdf