2013-12-17 31 views

回答

1

http手冊頁面,您可以找到以下

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. 

指示每當輸出重定向該http故意行爲不同。爲了獲得相同的行爲不重定向輸出,您可以使用

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

(但也注意到,漂亮的印刷與重定向行爲有所不同。)

+0

哎呀,看到這個答案被接受了,我想我的答案完全沒有了。在編寫Python程序時,我認爲這個問題是關於「如何將不同的結果打印到屏幕和文件」。 : - ] –

2

使用sys.stdout.isatty告訴stdout是否是終端(「TTY」)或文件,並根據打印不同的輸出,例如:

import sys 
if sys.stdout.isatty(): 
    print "Hello terminal!" 
else: 
    print "Hello non-terminal!"