os.isatty()

Returns True, if the file descriptior points to a tty.

In [1]: import os
In [2]: os.isatty(1)
Out[2]: True

To find out whether a process runs in the foreground, see here 15.6.2.