2

In order to prevent this from being a X Y problem, I will tell you the problem, and the proposed solutions that I cant figure out how to do.

I am trying to call the webbrowser module in a script.

import webbrowser
webbrowser.open_new_tab('http://stackoverflow.com')

Which is quite awesome. However, I want to be able to squash the input that it gives me. On my current machine(ubuntu) my browser(firefox) spits out a bunch of crap through stderr. I would like to be able to call webbrowser and eliminate the text from showing up.

What I have tried:

1 Use popen so that I can pipe the process to nothing.

from subprocess import Popen, PIPE
Popen(['python -m webbrowser \'' + url + '\''], stderr=PIPE,
          stdout=PIPE, shell=True)

Why this doesn't work

This actually works great in non-headless mode! When I use this in a ssh terminal (headless mode) the subprocess aspect doesn't allow me to see the headless terminal browser that is an oh so cool feature of webbrowser.

2 Redirect stderr

I cant just redirect stderr in python because the python script typically ends before the web browser is finished printing, so the process of the webbrowser continues to print in the terminal after the script has ended.

I have also tried: Suppressing output of module calling outside library with no luck

I cant use the suggestion in: suppress/redirect stderr when calling python webrowser because I want webbrowser to choose the default browser.

Possible solutions (only one of these would suffice):

Is there a way to know if I am in headless mode? If I could know that I was in an ssh/headless mode, I could use one method for headless mode, and the other for non headless.

Is there a way to do webbrowser.get('default').open() so that webbrowser doesnt give me the junk?

Is there a way to "pipe" the output of the browser without using subprocess?

Anything else that will solve the problem...

Community
  • 1
  • 1
Nick Humrich
  • 14,905
  • 8
  • 62
  • 85
  • I don't understand: `webbrowser` shouldn't try to start firefox in headless-mode; it should start a text browser such as lynx instead. See [How can I disable the webbrowser message in python?](http://stackoverflow.com/q/2323080/4279) – jfs Oct 17 '14 at 22:10
  • [`webbrowser` uses `DISPLAY`, `TERM`, `BROWSER` envvars](https://hg.python.org/cpython/file/tip/Lib/webbrowser.py#l473) to populate browser commands to run. – jfs Oct 17 '14 at 22:17

1 Answers1

0

To answer your (1) in possible solution:

import os     
def is_ssh(): 
    return "SSH_CLIENT" in os.environ or "SSH_TTY" in os.environ

In a screwy env, this may not always be set, but it should be.

For redirecting stderr, I'd think you could do:

import sys, StringIO
old_stderr = sys.stderr
temp_stderr = StringIO.StringIO()
try:
    # do your thing...
finally:
    sys.stderr = old_stderr

You say you can't do this because of when your process ends, but you could maybe do this without replacing the old stderr.

metaperture
  • 2,393
  • 1
  • 18
  • 19
  • The first solution worked. The second doesn't work all the time. – Nick Humrich Oct 16 '14 at 21:50
  • @Humdinger: `StringIO()` has no effect on browser's stderr that is in another process. See [Redirect stdout to a file in Python?](http://stackoverflow.com/a/22434262/4279) on how to redirect so that it works for subprocesses too (try `with stdout_redirected(stdout=sys.stderr): webbrowser...`). – jfs Oct 17 '14 at 22:11