0

In urllib2 how can I redirect the output a particular opener generates when I specify the debuglevel parameter?

E.g. for this example

opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=2))
opener.open("http://stackoverflow.com")

how would I catch the output in an "object" similar to sys.stderr and be able to write it anywhere I like?

I have a script that uses logging and stderr and this particular output should go to the log without hampering any of the other output.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • note: [the code shows that the output is printed to `sys.stdout`, not `sys.stderr`](https://hg.python.org/cpython/file/2.6/Lib/httplib.py#l405). – jfs Oct 30 '14 at 17:23
  • are there multiple threads in your program that may print while urllib2 makes requests? – jfs Oct 30 '14 at 17:48
  • @J.F.Sebastian: yep, unfortunately. Thanks for your answer and your time. I guess I'll have to use locking or a separate log file for the `urllib2` debugging. – 0xC0000022L Oct 30 '14 at 21:41

1 Answers1

1

The debug output is produced using print statements that print to sys.stdout.

It is easy to redirect sys.stdout temporarily for the duration of the urllib2 request but the effects are global i.e., if there are other Python threads are runnings then they'll see the change if they print something to sys.stdout.

You probably can't override what print statement does in Python 2.6. You can do it in Python 3 where print() is a function.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670