Foundations of Python Network Programming 978-1-4302-3004-5
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
CHAPTER 11 ■ WEB APPLICATIONS<br />
the sake <strong>of</strong> JavaScript routines running in each web page. If your application will require these, then<br />
check each framework that interests you for its degree <strong>of</strong> support for XML-RPC, JSON, and a RESTful<br />
approach to data documents in general.<br />
Obviously, user authentication is an enormous issue for many programmers. Some web frameworks<br />
remember usernames and passwords and feature support for login screens and session cookies out-<strong>of</strong>the-box;<br />
others make you build these components yourself or plug in a third-party approach to solve<br />
these issues. Still others have such complex systems <strong>of</strong> extensions and plug-ins that some developers<br />
can never get user configuration working in the desired manner.<br />
If you need specific advanced features such as a Comet-like approach to serving events back to the<br />
browser, then you should obviously look for a framework that implements those required features well.<br />
Note that some quite successful web applications are produced by combining two or more frameworks<br />
that are loosely coupled through a common database. For example, you might combine a Django<br />
application serving HTML pages with a restish application that provides RESTful CRUD operations that<br />
can be invoked from JavaScript. Choosing the best tool for the job sometimes involves combining<br />
several tools, instead <strong>of</strong> demanding everything from a single monolithic application.<br />
Finally, understand that experience is <strong>of</strong>ten the best guide to choosing a web framework. This<br />
means that, after all <strong>of</strong> the window shopping, you are going to have to sit down and try actually writing<br />
in some <strong>of</strong> the various frameworks available before you really know whether a given framework will<br />
prove a good fit for either yourself or your problem. We all like to think that we know ourselves well<br />
enough to predict such things without doing any actual coding. For example, we may feel we know<br />
intuitively which web framework is really “our style” and will let us be productive. However, trying a<br />
range <strong>of</strong> approaches may reveal that your initial guess about how your mind works did not actually do<br />
justice to what you can accomplish with an ostensibly “ugly” framework, once you understand its<br />
benefits.<br />
Remember that, whatever framework you choose, you are writing <strong>Python</strong> code. This means that<br />
your application's quality is going to depend at least as much upon your ability to write good, clean,<br />
<strong>Python</strong>ic code as it will upon any magical features <strong>of</strong> the framework itself. The framework might even<br />
feel rather awkward to you, or it might be selected by a senior team member with whom you disagree;<br />
but always remember that this is <strong>Python</strong>, and that its flexibility and elegance will generally let you write a<br />
good web application, whatever framework you ultimately end up using.<br />
Pure-<strong>Python</strong> Web Servers<br />
A fun way to demonstrate that <strong>Python</strong> comes with “batteries included” is to enter a directory on your<br />
system and run the SimpleHTTPServer Standard Library module as a stand-alone program:<br />
$ python -m SimpleHTTPServer<br />
Serving HTTP on 0.0.0.0 port 8000 ...<br />
If you direct your browser to localhost:8000, you will see the contents <strong>of</strong> this script's current<br />
directory displayed for browsing, such as the listings provided by Apache when a site leaves a directory<br />
browsable. Documents and images will load in your web browser when selected, based on the content<br />
types chosen through the best guesses <strong>of</strong> the mimetypes Standard Library module.<br />
The SimpleHTTPServer is a subclass <strong>of</strong> BaseHTTPServer, which is also the foundation <strong>of</strong> the<br />
wsgiref.simple_server that we looked at earlier. Before the WSGI standard was invented, small <strong>Python</strong><br />
programs could subclass BaseHTTPServer if they needed to answer raw HTTP requests. This is actually a<br />
common difference between the old mechanisms by which <strong>Python</strong> programs provided extensibility, and<br />
the more modern and <strong>Python</strong>ic mechanisms. It used to be popular to write a class with stub methods<br />
inside, and then tell programmers to extend it by subclassing and defining those methods; today, we use<br />
namespaces, callables, and duck-typed objects to provide much cleaner forms <strong>of</strong> extensibility. For<br />
example, today an object like start_response is provided as an argument (dependency injection), and<br />
192