HTTP: Accessing Web Sites
Python’s standard library (the modules that are installed with the interpreter) also includes client-side support for HTTP—the Hypertext Transfer Protocol—a message structure and port standard used to transfer information on the World Wide Web. In short, this is the protocol that your web browser (e.g., Internet Explorer, Netscape) uses to fetch web pages and run applications on remote servers as you surf the Web. Essentially, it’s just bytes sent over port 80.
To really understand HTTP-style transfers, you need to know some of the server-side scripting topics covered in Chapter 16 (e.g., script invocations and Internet address schemes), so this section may be less useful to readers with no such background. Luckily, though, the basic HTTP interfaces in Python are simple enough for a cursory understanding, even at this point in the book, so let’s take a brief look here.
Python’s standard httplib
module automates much of the protocol defined by HTTP and allows
scripts to fetch web pages much like web browsers. For instance, the
script in Example 14-29
can be used to grab any file from any server machine running an HTTP
web server program. As usual, the file (and descriptive header lines)
is ultimately transferred as formatted messages over a standard socket
port, but most of the complexity is hidden by the httplib
module.
Example 14-29. PP3E\Internet\Other\http-getfile.py
####################################################################### # fetch a ...
Get Programming Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.