Вопрос

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