문제

I need to run tcpdump, kill it after a bit and read from stderr. The following code works in Python 2.7, but I need to make it work in Python 2.5 too::

tcpdumpProcess = subprocess.Popen(['sudo', '/usr/sbin/tcpdump',
                        '-w', 'dumpedTraffic.pcap',
                        '-n', 'ip'],
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
tcpdumpProcess.terminate()    
tcpdumpProcess.wait()
tcpdump_stderr =  tcpdumpProcess.communicate()[1]

Python 2.5 complains that:

tcpdumpProcess.terminate() AttributeError: 'Popen' object has no attribute 'terminate'

What's en equivalent way of doing it in Python 2.5?

도움이 되었습니까?

해결책

You can use os.kill method to terminate the process.

os.kill(tcpdumpProcess.pid, signal.SIGTERM)

다른 팁

You can execute the Kill command along with processid as same as u execute the previous command.

or

u can use the kill methods provided in the os module along with the process id and the signal status such as TERM etc.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top