1

I have noticed that the httpie python tool gives different results in the following two caes:

  1. $ http google.com
  2. $ http google.com > out.txt

The file out.txt misses headers that are present in the first case.

1
  • I am a little confused as to what the error is exactly. Commented Dec 17, 2013 at 12:54

2 Answers 2

2

Use sys.stdout.isatty to tell whether stdout is a terminal (a "tty") or a file and print different output depending on that, e.g.:

import sys
if sys.stdout.isatty():
    print "Hello terminal!"
else:
    print "Hello non-terminal!"
Sign up to request clarification or add additional context in comments.

Comments

1

On the manual page of http you can find the following

Output options:
 --print WHAT, -p WHAT

   String specifying what the output should contain:

   'H' request headers 'B' request body 'h' response headers 'b' response body

   The default behaviour is 'hb' (i.e., the response headers  and  body  is 
   printed), if standard  output  is  not redirected. If the output is piped
   to another program or to a file, then only the response body is printed by
   default.

Which indicates that http intentionally behaves differently whenever the output is redirected. To obtain the same behavior as for not redirected output you can use

`http --print hb google.com > out.txt`

(But also note that pretty-printing behaves differently with redirection.)

1 Comment

Oops, seeing that this answer was accepted, I guess my answer was totally off. I thought the question is about "how to print different results to a screen and to a file" when writing a Python program. :-]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.