How do I get urllib2 to log ALL transferred bytes
Asked Answered
C

2

19

I'm writing a web-app that uses several 3rd party web APIs, and I want to keep track of the low level request and responses for ad-hoc analysis. So I'm looking for a recipe that will get Python's urllib2 to log all bytes transferred via HTTP. Maybe a sub-classed Handler?

Cache answered 23/7, 2009 at 9:56 Comment(1)
+1 Very nice python question asked almost 2 years ago with no upvotes. It's really odd. I'm looking for an answer myself. In my case I'm concerned with HTTP headers only, however.Mercola
C
12

Well, I've found how to setup the built-in debugging mechanism of the library:

import logging, urllib2, sys

hh = urllib2.HTTPHandler()
hsh = urllib2.HTTPSHandler()
hh.set_http_debuglevel(1)
hsh.set_http_debuglevel(1)
opener = urllib2.build_opener(hh, hsh)
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.NOTSET)

But I'm still looking for a way to dump all the information transferred.

Cache answered 25/7, 2009 at 8:22 Comment(0)
S
2

This looks pretty tricky to do. There are no hooks in urllib2, urllib, or httplib (which this builds on) for intercepting either input or output data.

The only thing that occurs to me, other than switching tactics to use an external tool (of which there are many, and most people use such things), would be to write a subclass of socket.socket in your own new module (say, "capture_socket") and then insert that into httplib using "import capture_socket; import httplib; httplib.socket = capture_socket". You'd have to copy all the necessary references (anything of the form "socket.foo" that is used in httplib) into your own module, but then you could override things like recv() and sendall() in your subclass to do what you like with the data.

Complications would likely arise if you were using SSL, and I'm not sure whether this would be sufficient or if you'd also have to make your own socket._fileobject as well. It appears doable though, and perusing the source in httplib.py and socket.py in the standard library would tell you more.

Slung answered 4/12, 2009 at 3:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.