Python子进程输出读取错误
我有一个命令在航站楼非常有效:Python子进程输出读取错误,python,subprocess,output,tshark,Python,Subprocess,Output,Tshark,我有一个命令在航站楼非常有效: sudo tshark -V -l -i "any" -f 'udp port 4729' 我正在尝试读取python脚本的输出: import subprocess command = ['tshark', '-V', '-l', '-i', '"any"', '-f', '"udp port 4729"'] # the shell command process = subprocess.Popen(command, stdout=subprocess.P
sudo tshark -V -l -i "any" -f 'udp port 4729'
我正在尝试读取python脚本的输出:
import subprocess
command = ['tshark', '-V', '-l', '-i', '"any"', '-f', '"udp port 4729"'] # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None)
output, error = process.communicate()
print output
它不起作用。可能是在列表中编写命令时遇到了一些问题
我收到错误消息:
gooman@ubuntu:~/workspace/glade_tests/src$ sudo ./main.py
tshark: Lua: Error during loading:
[string "/usr/share/wireshark/init.lua"]:45: dofile has been disabled
Running as user "root" and group "root". This could be dangerous.
Capturing on "any"
tshark: The capture session could not be initiated (No such device exists).
Please check to make sure you have sufficient permissions, and that you have the proper interface or pipe specified.
0 packets captured
这是:
import subprocess
command = "sudo tshark -V -l -i "any" -f 'udp port 4729'"
try:
output = subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as e:
print "An error has been occured", e
raise
print "The subprocess output:", output
可能需要添加stdout=subprocess.PIPE参数。如果将sudo添加到列表
['sudo',tshark',…]
中,并将stderr=None,shell=True
添加到Popen
中,会发生什么情况?为什么要使用subprocess.Popen
而不是简单的subprocess.call
?如果使用子流程调用
,会发生什么情况?另外,请注意,Popen
启动一个,因此使用call
进行调试应该更容易。请尝试output=subprocess。检查输出(命令)
@User:不要使用shell=True
和列表参数。大多数情况下这是一个错误。@Gooman:drop“
在参数内部,没有shell来解释它们,即使用output=subprocess。检查输出(shlex.split(““sudo tshark-V-l-i”any“-f'udp port 4729'))