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

(yzsuai) #1

introduction to network socket addresses in Chapter 12 and the discussion of URLs in
Chapter 15.


If you do install this example’s code on a different server, simply replace the “localhost:
8000/cgi-bin” part of the URLs we’ll use here with your server’s name, port, and path
details. In practice, a system such as PyMailCGI would be much more useful if it were
installed on a remote server, to allow mail processing from any web client.*


As with PyMailGUI, you’ll have to edit the mailconfig.py module’s settings to use this
system to read your own email. As provided, the email server information is not useful
for reading email of your own; more on this in a moment.


Carry-On Software
PyMailCGI works as planned and illustrates more CGI and email concepts, but I want
to point out a few caveats up front. This application was initially written during a two-
hour layover in Chicago’s O’Hare airport (though debugging took a few hours more).
I wrote it to meet a specific need—to be able to read and send email from any web
browser while traveling around the world teaching Python classes. I didn’t design it to
be aesthetically pleasing to others and didn’t spend much time focusing on its efficiency.
I also kept this example intentionally simple for this book. For example, PyMailCGI
doesn’t provide nearly as many features as the PyMailGUI program in Chapter 14, and
it reloads email more than it probably should. Because of this, its performance can be
very poor if you keep your inbox large.
In fact, this system almost cries out for more advanced state retention options. As is,
user and message details are passed in generated pages as hidden fields and query pa-
rameters, but we could avoid reloading mail by also using server-side deployment of
the database techniques described in Chapter 17. Such extensions might eventually
bring PyMailCGI up to the functionality of PyMailGUI, albeit at some cost in code
complexity. Even so, this system also suffers from the Python 3.1 attachments limitation
described earlier, which would need to be addressed as well.
Again, you should consider this system a prototype and a work in progress; it’s not yet
software worth selling, and not something that you’ll generally want to use as is for
mail that’s critical to you. On the other hand, it does what it was intended to do, and
you can customize it by tweaking its Python source code—something that can’t be said
of all software sold.


  • One downside to running a local webserver.py script that I noticed during development for this chapter is
    that on platforms where CGI scripts are run in the same process as the server, you’ll need to stop and restart
    the server every time you change an imported module. Otherwise, a subsequent import in a CGI script will
    have no effect: the module has already been imported in the process. This is not an issue on Windows today
    or on other platforms that run the CGI as a separate, new process. The server’s classes’ implementation varies
    over time, but if changes to your CGI scripts have no effect, your platform my fall into this category: try
    stopping and restarting the locally running web server.


1238 | Chapter 16: The PyMailCGI Server

Free download pdf