import os, tempfile, subprocess URL_OPENER = 'xdg-open' def open_response_content(response): """ Saves a response's content to a temporary file and opens it in a browser. """ temp_file = tempfile.NamedTemporaryFile(delete=False) temp_file.write(response.content) dev_null = open(os.devnull, 'w') kwargs = {'stdout': dev_null, 'stderr': dev_null} subprocess.Popen([URL_OPENER, temp_file.name], **kwargs)