2012-09-07 59 views
7

我知道我可以在python像获取进程的stdin使用子:如何通过进程ID获取进程的stdin?

import subprocess 
f = subprocess.Popen('python example.py',stdin=subprocess.PIPE) 
f.stdin.write('some thing') 

,但我只想知道我想写的PID进程的stdin 我怎么能做到这一点?

+3

你想劫持另一个进程的标准输入?这不是一件很好的事情... –

+0

是的,我想写一些someting到其他进程的标准输入 – timger

+0

你想要什么机器? – 0x90

回答

11

简单地写/proc/PID/fd/1

import os.path 
pid = os.getpid() # Replace your PID here - writing to your own process is boring 
with open(os.path.join('/proc', str(pid), 'fd', '1'), 'a') as stdin: 
    stdin.write('Hello there\n')