proc = subprocess.Popen(['sort'], stdin=subprocess.PIPE, stdout=subprocess.PIPE
    )
proc.stdin.write('foo\n')
proc.stdin.write('bar\n')
out, err = proc.communicate()
subprocess replacement of popen2 with Python