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

(yzsuai) #1

In terms of implementation, though, the model is very different. PyMailGUI doesn’t
need to care about things such as passing state in URLs or hidden fields (it saves state
in Python in-process variables and memory), and there’s no notion of escaping HTML
and URL strings (there are no browsers, and no network transmission steps once mail
is downloaded). It also doesn’t have to rely on temporary server file links to give access
to message parts—the message is retained in memory attached to a window object and
lives on between interactions. On the other hand, PyMailGUI does require Python to
be installed on the client, but we’ll return to that in a few pages.


Processing Fetched Mail


At this point in our PyMailCGI web interaction, we are viewing an email message
(Figure 16-12) that was chosen from the selection list page. On the message view page,
selecting an action from the pull-down list and clicking the Next button invokes the
script in Example 16-9 on the server to perform a reply, forward, or delete operation
for the selected message viewed.


Example 16-9. PP4E\Internet\Web\PyMailCgi\cgi-bin\onViewPageAction.py


#!/usr/bin/python
"""
################################################################################
On submit in mail view window: action selected=(fwd, reply, delete);
in 2.0+, we reuse the mailtools delete logic originally coded for PyMailGUI;
################################################################################
"""


import cgi, commonhtml, secret
from externs import mailtools, mailconfig
from commonhtml import getfield


def quotetext(form):
"""
note that headers come from the prior page's form here,
not from parsing the mail message again; that means that
commonhtml.viewpage must pass along date as a hidden field
"""
parser = mailtools.MailParser()
addrhdrs = ('From', 'To', 'Cc', 'Bcc') # decode name only
quoted = '\n-----Original Message-----\n'
for hdr in ('From', 'To', 'Date'):
rawhdr = getfield(form, hdr)
if hdr not in addrhdrs:
dechdr = parser.decodeHeader(rawhdr) # 3.0: decode for display
else: # encoded on sends
dechdr = parser.decodeAddrHeader(rawhdr) # email names only
quoted += '%s: %s\n' % (hdr, dechdr)
quoted += '\n' + getfield(form, 'text')
quoted = '\n' + quoted.replace('\n', '\n> ')
return quoted


1266 | Chapter 16: The PyMailCGI Server

Free download pdf