473,513 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sending Cntrl-C ??

Hi,

I really like this recipe for controlling subprocesses:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

However, I can't figure out how I can send the equivalent of "Cntrl-C"
to the subprocess. How can that be done?

TIA,
-T
Jun 27 '08 #1
7 14149
gamename schrieb:
Hi,

I really like this recipe for controlling subprocesses:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

However, I can't figure out how I can send the equivalent of "Cntrl-C"
to the subprocess. How can that be done?
import os
import signal
import subprocess

popen = subprocess(...)
os.kill(popen.pid, signal.SIGINT)

Or with Python 2.6+:

popen.send_signal(signal.SIGINT)

Christian

Jun 27 '08 #2
import os
import signal
import subprocess

popen = subprocess(...)
os.kill(popen.pid, signal.SIGINT)

Or with Python 2.6+:

popen.send_signal(signal.SIGINT)
Thanks, Christian. Would that work on win32 as well?

-T
Jun 27 '08 #3
gamename schrieb:
Thanks, Christian. Would that work on win32 as well?
No, Windows doesn't support the same, rich set of signal as Unix OSes.

Christian

Jun 27 '08 #4
En Tue, 29 Apr 2008 20:48:42 -0300, Christian Heimes <li***@cheimes.de>
escribió:
gamename schrieb:
>Thanks, Christian. Would that work on win32 as well?

No, Windows doesn't support the same, rich set of signal as Unix OSes.
True but irrelevant to the question.
To the OP: you can download the pywin32 package from sourceforge, and use
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_ EVENT, pgid)
or call the same function using ctypes.
See http://msdn.microsoft.com/en-us/libr...55(VS.85).aspx for some
important remarks.

--
Gabriel Genellina

Jun 27 '08 #5
>
No, Windows doesn't support the same, rich set of signal as Unix OSes.

True but irrelevant to the question.
To the OP: you can download the pywin32 package from sourceforge, and use
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_ EVENT, pgid)
or call the same function using ctypes.
Seehttp://msdn.microsoft.com/en-us/library/ms683155(VS.85).aspxfor some
important remarks.
Thanks, guys. Good info.
-T
Jun 27 '08 #6
win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C_ EVENT, pgid)

How do you determine the value of 'pgid'?

Jun 27 '08 #7
En Wed, 30 Apr 2008 15:06:13 -0300, gamename <na***************@yahoo.com>
escribió:
>win32api.GenerateConsoleCtrlEvent(win32con.CTRL_C _EVENT, pgid)

How do you determine the value of 'pgid'?
Make the child start a new process group, then its pid is the process
group ID. You have to use the "creationflags" parameter of subprocess.open
The documentation for GenerateConsoleCtrlEvent
http://msdn.microsoft.com/en-us/library/ms683155.aspx states that you
can't send CTRL_C_EVENT to another process group, only CTRL_BREAK_EVENT
(and only to the same console as the sender process). A little example:

<code>
import subprocess
import ctypes
import time

CREATE_NEW_PROCESS_GROUP = 512
CTRL_C_EVENT = 0
CTRL_BREAK_EVENT = 1
GenerateConsoleCtrlEvent = ctypes.windll.kernel32.GenerateConsoleCtrlEvent

print "start child process"
p = subprocess.Popen("cmd /c for /L %d in (10,-1,0) do @(echo %d && sleep
1)",
creationflags = CREATE_NEW_PROCESS_GROUP)
print "pid=", p.pid
print "wait 3 secs"
time.sleep(3)
print "send Ctrl-Break"
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, p.pid)
print "wait for child to stop"
print "retcode=", p.wait()
print "done"
</code>

Output:

start child process
pid= 872
wait 3 secs
10
9
8
7
send Ctrl-Break
wait for child to stop
retcode= 255
done

(Instead of ctypes and those magical constants, you can install the
pywin32 package and use win32api.GenerateConsoleCtrlEvent,
win32con.CTRL_BREAK_EVENT and win32process.CREATE_NEW_PROCESS_GROUP)

The only way I know of to send a Ctrl-C event to a different console
involves remote code injection.

--
Gabriel Genellina

Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
14485
by: coder_1024 | last post by:
I'm trying to send a packet of binary data to a UDP server. If I send a text string, it works fine. If I attempt to send binary data, it sends a UDP packet with 0 bytes of data (just the...
0
522
by: praba kar | last post by:
Dear All, I have doubt regarding mail sending smtplib module. The below code is I used to send a mail. ########################################## import email.Message import email.Utils...
10
4944
by: Stuart Mueller | last post by:
I have an exchange server, that I sometimes use to perform mail shots to clients on our database, these can be upwards of 1000 at a time. As we don't want different clients to see who we are...
3
4614
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
4
8189
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
3
3596
by: Ant | last post by:
Hi, I'm using the MailMessage & smtpMail classes in System.Web.Mail to send mail, however it's not sending any emails. I'm using it on a Windows 2003 server. The simplest way to use this is...
0
1850
by: remya1000 | last post by:
by using FTP i can send files to server using vb.net. if the file is big, then it will take some time to complete the sending process to server.or if we were sending 3-4 files to the server one by...
9
3434
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
4
2380
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I use: Dim message As New MailMessage(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text) Dim emailClient As New SmtpClient(txtSMTPServer.Text) emailClient.Send(message) And its...
6
9187
by: Chocolade | last post by:
Hi, Im using System.Net.Mail to send email in my application it was working great without any problems untill this morning after like 20-30 tries it was sending the email ok then suddenly this...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.