class Silent:
def write(self, line): pass
save_stdout = sys.stdout
sys.stdout = Silent() # send print to dummy object
try: # which has a write method
result = func(*args) # try to return func result
finally: # but always restore stdout
sys.stdout = save_stdout
return result
def dumpstatepage(exhaustive=0):
"""
for debugging: call me at top of a CGI to
generate a new page with CGI state details
"""
if exhaustive:
cgi.test() # show page with form, environ, etc.
else:
pageheader(kind='state dump')
form = cgi.FieldStorage() # show just form fields names/values
cgi.print_form(form)
pagefooter()
sys.exit()
def selftest(showastable=False): # make phony web page
links = [ # [(text, url, {parms})]
('text1', urlroot + 'page1.cgi', {'a':1}),
('text2', urlroot + 'page1.cgi', {'a':2, 'b':'3'}),
('text3', urlroot + 'page2.cgi', {'x':'a b', 'y':'a<b&c', 'z':'?'}),
('te<>4', urlroot + 'page2.cgi', {'
pageheader(kind='View')
if showastable:
pagelisttable(links)
else:
pagelistsimple(links)
pagefooter()
if name == 'main': # when run, not imported
selftest(len(sys.argv) > 1) # HTML goes to stdout
Web Scripting Trade-Offs
As shown in this chapter, PyMailCGI is still something of a system in the making, but
it does work as advertised: when it is installed on a remote server machine, by pointing
a browser at the main page’s URL, I can check and send email from anywhere I happen
to be, as long as I can find a machine with a web browser (and can live with the limi-
tations of a prototype). In fact, any machine and browser will do: Python doesn’t have
to be installed anew, and I don’t need POP or SMTP access on the client machine itself.
That’s not the case with the PyMailGUI client-side program we wrote in Chapter 14.
This property is especially useful at sites that allow web access but restrict more direct
protocols such as POP email.
Web Scripting Trade-Offs| 1291