简体   繁体   中英

output of os.system in variable python

I am making a server that asks for you IP address and I want to make an option that says (leave blank for __your ip__) but I'm having trouble with it. I tried doing this

import subprocess
output = subprocess.check_output("ipconfig getifaddr en0", shell=True)

but it returns b'***.***.***.***\n' is there a way to remove the '', b and \n or another way to get an IP that doesn't return 127.0.0.1 ?

What you're seeing is the bytestring output and the following newline.

You'll want

output = subprocess.check_output("ipconfig getifaddr en0", shell=True).decode().strip()

to first decode the bytes as UTF-8, then strip whitespace from the start and the end.

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