資料內(nèi)容:
In many cases, network programming simply involves selecting and using a library that already
supports the network operations you need to perform. A major purpose of this book is to introduce you
to all of the key networking libraries available for Python, and to teach you about the lower-level network
services on which those libraries are built—both so that you understand how the libraries work, and so
that you will understand what is happening when something at a lower level goes wrong.
Let’s begin with a very simple example. I have here a mailing address, which looks like this:
207 N. Defiance St
Archbold, OH
And I am interested in knowing the latitude and longitude of this physical address. It just so
happens that Google provides a “Maps API” that can perform such a conversion. What would I have to
do to take advantage of this network service from Python?
When looking at a new network service that you want to use, it is always worthwhile to start by
finding out whether someone has already implemented the protocol—in this case, the Google Maps
protocol—that your program will need to speak. Start by scrolling through the Python Standard Library
documentation, looking for anything having to do with Google Maps:
http://docs.python.org/library/
Do you see anything? No, neither do I. But it is important for a Python programmer to look through
the Standard Library’s table of contents pretty frequently, even if you usually do not find what you are
looking for, because each reading will make you more familiar with the services that do come included
with Python.
Since the Standard Library does not have a package to help us, we can turn to the Python Package
Index, an excellent resource for finding all sorts of general-purpose Python packages contributed by
other programmers and organizations from across the world. You can also, of course, check the web site
of the vendor whose service you will be using to see whether they provide a python library to access it.
Or you can do a general Google search for “Python” plus the name of whatever web service you want to
use, and see whether any of the first few results link to a package that you might want to try.
In this case, I searched the Python Package Index, which lives at this URL:
http://pypi.python.org/
There, I did a search for Google maps, and immediately found a package that is actually named
googlemaps and that provides a clean interface to its features (though, you will note from its description,
it is not vendor-provided, but was instead written by someone besides Google):
http://pypi.python.org/pypi/googlemaps/
This is such a common situation—that you find a Python package that sounds like it might already
do exactly what you want, and that you want to try it out on your system—that we should pause for a
moment and introduce you to the very best Python technology for quickly trying out a new library:
virtualenv!
In the old days, installing a Python package was a gruesome and irreversible act that required
administrative privileges on your machine and left your system Python install permanently altered. After
several months of heavy Python development, your system Python install could become a wasteland of
dozens of packages, all installed by hand, and you could even find that the new packages you tried to
install would break because they were incompatible with one of the old packages sitting on your hard
drive from a project that ended months ago.