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

(yzsuai) #1

We’ll discuss configuration details as we move along, but in short, when a Python script
opens an Internet connection (with the socket module or one of the Internet protocol
modules), Python will happily use whatever Internet link exists on your machine, be
that a dedicated T1 line, a DSL line, or a simple modem. For instance, opening a socket
on a Windows PC automatically initiates processing to create a connection to your
Internet provider if needed.


Moreover, as long as your platform supports sockets, you probably can run many of
the examples here even if you have no Internet connection at all. As we’ll see, a machine
name localhost or "" (an empty string) usually means the local computer itself. This
allows you to test both the client and the server sides of a dialog on the same computer
without connecting to the Net. For example, you can run both socket-based clients and
servers locally on a Windows PC without ever going out to the Net. In other words,
you can likely run the programs here whether you have a way to connect to the Internet
or not.


Some later examples assume that a particular kind of server is running on a server
machine (e.g., FTP, POP, SMTP), but client-side scripts themselves work on any
Internet-aware machine with Python installed. Server-side examples in Chapters 15
and 16 require more: to develop CGI scripts, you’ll need to either have a web server
account or run a web server program locally on your own computer (which is easier
than you may think—we’ll learn how to code a simple one in Python in Chapter 15).
Advanced third-party systems like Jython and Zope must be downloaded separately,
of course; we’ll peek at some of these briefly in this chapter but defer to their own
documentation for more details.


In the Beginning There Was Grail
Besides creating the Python language, Guido van Rossum also wrote a World Wide
Web browser in Python years ago, named (appropriately enough) Grail. Grail was
partly developed as a demonstration of Python’s capabilities. It allows users to browse
the Web much like Firefox or Internet Explorer, but it can also be programmed with
Grail applets—Python/tkinter programs downloaded from a server when accessed and
run on the client by the browser. Grail applets work much like Java applets in more
widespread browsers (more on applets in the next section).
Though it was updated to run under recent Python releases as I was finishing this
edition, Grail is no longer under active development today, and it is mostly used for
research purposes (indeed, the Netscape browser was counted among its contempo-
raries). Nevertheless, Python still reaps the benefits of the Grail project, in the form of
a rich set of Internet tools. To write a full-featured web browser, you need to support
a wide variety of Internet protocols, and Guido packaged support for all of these as
standard library modules that were eventually shipped with the Python language.
Because of this legacy, Python now includes standard support for Usenet news (NNTP),
email processing (POP, SMTP, IMAP), file transfers (FTP), web pages and interactions
(HTTP, URLs, HTML, CGI), and other commonly used protocols such as Telnet.

776 | Chapter 12: Network Scripting

Free download pdf