...

/

What’s On The Wire?

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).

Press + to interact
from http.client import HTTPConnection
HTTPConnection.debuglevel = 1 #①
from urllib.request import urlopen
response = 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 ...

Access this course and 1400+ top-rated courses and projects.