getting all HTTP headers from urllib2 Request?

cgoldberg cgoldberg at gmail.com
Mon Mar 2 19:38:45 EST 2009


I have a Python web client that uses urllib2.  It is easy enough to
add my own HTTP headers to the outgoing requests. I just create a
dictionary of the headers I want to add, and pass it to the Request
initializer.

These custom headers are not all that gets sent.  urllib2 attaches
headers also.  You can view the headers that urrlib2 adds by looking
at unredirected_hdrs.

However, these aren't the only HTTP headers that get sent on the
wire.  Other standard HTTP headers get added to outgoing requests as
well as the custom ones I explicitly add and urllib2 adds. When I
sniff the request using Wireshark, I see headers besides the ones I
added myself.

My question is how do a I get access to *all* of these headers?  I
want to log every request (including the full set of HTTP headers that
get sent) that gets sent from my program, and can't figure out how.
any pointers?

In a nutshell: How do I get *all* the outgoing headers from an HTTP
request created by urllib2?


- Corey Goldberg





More information about the Python-list mailing list