philihpAboutPGPLightning

Debugging Urllib2 in Python

Philihp Busby,0 min read

I was having some trouble debugging an HTTP request using urllib2 in Python which did some unorthodox things with HTTP headers. The urllib2 module itself doesn't have much debug facility. You can see response headers by looking at the hdrs attribute on the exception like this

try: opener.open(

But in order to see the actual request string, you've got to go a layer deeper. Urllib2 runs on top of Httplib, so do this before you make your request

import httplib httplib.HTTPConnection.debuglevel = 1

GitHub · Bluesky · LinkedIn · Instagram · KeybaseRSS

Built from 8f0906ac CC BY 4.0 — with love from San Francisco.