简体   繁体   English

在Python 2.5中执行shell进程

[英]execute shell process in Python 2.5

I need to run tcpdump , kill it after a bit and read from stderr. 我需要运行tcpdump ,稍后将其tcpdump并从stderr读取。 The following code works in Python 2.7 , but I need to make it work in Python 2.5 too:: 以下代码适用于Python 2.7 ,但我需要使它在Python 2.5中工作::

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: Python 2.5抱怨说:

tcpdumpProcess.terminate() AttributeError: 'Popen' object has no attribute ' terminate ' tcpdumpProcess.terminate()AttributeError:'Popen'对象没有属性' terminate '

What's en equivalent way of doing it in Python 2.5? 什么是在Python 2.5中这样做的等效方式?

您可以使用os.kill方法终止进程。

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

You can execute the Kill command along with processid as same as u execute the previous command. 您可以像执行上一个命令一样执行Kill命令和processid。

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. 你可以使用os模块中提供的kill方法以及进程id和信号状态,如TERM等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM