This may have portability problems, but in most Unix-like environments it /dev/stdoutrefers to the current terminal and is unique to every connection [windows, tabs, ssh, etc.] that you opened. Given these assumptions, you can use:
with open('/dev/stdout') as fd:
tty_path = os.ttyname(fd.fileno())
source
share