What’s On The Wire?
We'll cover the following...
To see why this is inefficient and rude, let’s turn on the debugging features of Python’s HTTP library and see what’s being sent “on the wire” (i.e. over the network).
from http.client import HTTPConnectionHTTPConnection.debuglevel = 1 #①from urllib.request import urlopenresponse = urlopen('http://diveintopython3.org/examples/feed.xml') #②#send: b'GET /examples/feed.xml HTTP/1.1 #③#Host: diveintopython3.org #④#Accept-Encoding: identity #⑤#User-Agent: Python-urllib/3.1' #⑥#Connection: close#reply: 'HTTP/1.1 200 OK'#…further debugging information omitted…
① As I mentioned at the beginning of the chapter, urllib.request
relies on another standard Python library, http.client
. Normally you don’t need to touch http.client
directly. (The urllib.request
module imports it automatically.) But we import it here so we can toggle the debugging flag on the HTTPConnection
class that urllib.request
uses to connect to the HTTP server.
② Now that the debugging flag is set, information on the HTTP request and response is printed out in real time. As you can see, when you request the Atom feed, the urllib.request
module sends five lines to the server.
③ The first line specifies the HTTP verb ...