attach to process by pid?
Grant Edwards
invalid at invalid.invalid
Sat Mar 12 12:21:44 EST 2011
On 2011-03-12, John Nagle <nagle at animats.com> wrote:
> On 3/9/2011 5:38 PM, Miki Tebeka wrote:
>>> Is there any way to attach to an already running process by pid? I
>>> want to send commands from python to an application that is already
>>> running. I don't want to give the command name to
>>> subprocess.Popen.
>> We probably need more information. What do you mean by "send
>> commands"? (What was your work flow if you could give the command
>> name to Popen?)
>
> There are ways under Windows to attach to a running process and do
> things to it. Look up "CreateRemoteThread". There's a way to
> do this under Linux, too. See
> "http://www.codeproject.com/KB/DLL/code_injection.aspx".
The basic method being used in the recipe above to "mess with" the
target process is the ptrace system call (which is how the debugger
manipulates the state of the target process). There are probably
other less indirect ways of using ptrace to "hijack" stdin, but
running code in the context of the target process that creates a pipe
and attaches stdin to it might work.
> It is very unlikely that a Python programmer would use either of
> these. The original poster needs to express more clearly what
> they really want to do. I suspect they want to create a server
> that client programs can talk to. There are good ways to do that,
> both on the same machine and across a network.
Oh, Certainly. The OP's question was so vague that I think we all
know this is just an academic discussion and has nothing (practical)
to do with the OP.
--
Grant
More information about the Python-list
mailing list