Sending <Ctrl-C> to a process opened from popen()
Trent Mick
trentm at ActiveState.com
Wed Aug 20 15:10:31 EDT 2003
[Marc wrote]
> Hi all,
>
> In searching for an answer to this problem I found several solutions
> dealing with Unix but none dealing with Windows. Basically I want to
> send a ping for an indefinite period of time, stop it at some point,
> and then read the results. I open up a ping process using popen().
>
> I found ways to send a break signal using signal module, but for that
> I need the PID. All the ways described in the newsgroups for finding
> PID were for UNIX.
>
> So how do I find the PID of a spawned process in Windows to send a
> break signal?
You want to use the Win32 API method GenerateConsoleCtrlEvent:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/generateconsolectrlevent.asp
The PyWin32 extensions (comes with ActivePython, can be installed
separately on other Python distros) provide access to this:
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_EVENT, pid)
But you need to get the pid for that. You already have the PID if you
started a process with CreateProcess, but you are using popen, which
hides those details. I don't know an easy way to get the PID.
I have a process.py module which will spawn a process and return a
object represent it with attributes for stdin/stdout/stderr and methods
to .wait(), .kill(). This might be more helpful to you, but my
process.py is not perfect by any stretch.
http://starship.python.net/~tmick/#process
Cheers,
Trent
--
Trent Mick
TrentM at ActiveState.com
More information about the Python-list
mailing list