简体   繁体   中英

subprocess.check_output still print in stdout

i want to get the stdout output in a variable from commande after some research i found that subprocess.check_output(cmd) do this well ! but it still print me some output in stdout !

cmd = ["openssl","rsa", "-in","pubkey.pem","-pubin","-text","-modulus"]
output = subprocess.check_output(cmd)

i got what i want in my output variable but it still print me

writing RSA key

and in the output var i have

'Public-Key: (576 bit)\nModulus:\n    00:c2:cb:b2:4f:db:f9:23:b6:12:68:e3:f1:1a:38:\n    96:de:45:74:b3:ba:58:73:0c:bd:65:29:38:86:4e:\n    22:23:ee:eb:70:4a:17:cf:d0:8d:16:b4:68:91:a6:\n    14:74:75:99:39:c6:e4:9a:af:e7:f2:59:55:48:c7:\n    4c:1d:7f:b8:d2:4c:d1:5c:b2:3b:4c:d0:a3\nExponent: 65537 (0x10001)\nModulus=C2CBB24FDBF923B61268E3F11A3896DE4574B3BA58730CBD652938864E2223EEEB704A17CFD08D16B46891A61474759939C6E49AAFE7F2595548C74C1D7FB8D24CD15CB23B4CD0A3\n-----BEGIN PUBLIC KEY-----\nMGQwDQYJKoZIhvcNAQEBBQADUwAwUAJJAMLLsk/b+SO2Emjj8Ro4lt5FdLO6WHMM\nvWUpOIZOIiPu63BKF8/QjRa0aJGmFHR1mTnG5Jqv5/JZVUjHTB1/uNJM0VyyO0zQ\nowIDAQAB\n-----END PUBLIC KEY-----\n'

the content of pubkey.pem is :

-----BEGIN PUBLIC KEY-----
MGQwDQYJKoZIhvcNAQEBBQADUwAwUAJJAMLLsk/b+SO2Emjj8Ro4lt5FdLO6WHMM
vWUpOIZOIiPu63BKF8/QjRa0aJGmFHR1mTnG5Jqv5/JZVUjHTB1/uNJM0VyyO0zQ
owIDAQAB
-----END PUBLIC KEY-----

Thanx for help :D

check_output lets you redirect stderr to a file descriptor. I've redirected stderr to '/dev/null` below and I think it gives you what you want:

dev_null = open(os.devnull, 'w')
output = subprocess.check_output(cmd, stderr=dev_null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM