我正在尝试使用subprocess.Popen
来运行'cat test.txt | grep txt',但它不起作用。在我的代码中,我执行了两次subprocess.Popen
命令。
1:我第一次用它来运行一个tshark命令,它将命令输出重定向到一个text(test.txt)文件(工作正常)。 (在下面的代码中的函数get_all_tshark_out中定义)
2:第二次使用subprocess.Popen运行'cat test.txt | grep txt'命令从此文件中提取txt以执行某些验证。这不适合我。 (在下面的代码中的函数get_uniq_sessions中定义)
为了确保它不是因为缓冲区溢出,我也在冲洗stdout
,但没有得到任何帮助。以下是我的代码:
import subprocess
import logging
def get_all_tshark_out(logger, tcpdump, port):
command = """tshark -r "%s" -odiameter.tcp.ports:"%s" -R 'diameter.cmd.code == 272 and diameter.flags.request==0 and !tcp.analysis.retransmission and diameter.flags.T == 0' -Tpdml -Tfields -ediameter.Session-Id | sort > test.txt""" %(tcpdump, port)
p_out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
sys.stdout.flush()
sys.stderr.flush()
return 1
def get_uniq_sessions(logger, id='1234', uniqlog_file='test.txt'):
command = "cat "+ uniqlog_file +" | grep "+ id
print command
p_out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print "PPPPP", p_out
output = p_out.stdout.read()
p_out.wait()
command_out_list = (output.strip().split("\n"))
sys.stdout.flush()
print "%%%", output, p_out.stderr.read()
print len(command_out_list)
if p_out.stderr.read():
logger.error("\"%s\" Error happened while trying to execute \"%s\"" (p_out.stderr.read().strip(), command))
sys.exit(1)
elif command_out_list[0] == '' and len(command_out_list) == 1:
logger.error("No Sessions belongs to %s campaign ID please provide proper input as Campaign ID" %id)
sys.exit(1)
else:
return command_out_list
我该如何解决?
TL; DR你的两个subprocess.Popen()
电话都被打破了;改为使用subprocess
中的一个包装器方法,和/或使用Python的内置工具而不是外部工具。
您使用useless use of cat
有特殊原因吗?只需subprocess.Popen(['grep', id, uniqlog_file])
会更简单,并且不需要shell=True
- 当然,Python本身非常适合读取文件并检查每行是否包含字符串。
def get_uniq_sessions(logger, id='1234', uniqlog_file='test.txt'):
matches = []
with open(uniqlog_file, 'r') as handle:
for line in handle:
if id in line:
matches.append(line)
return matches
你的功能可能不应该调用sys.exit()
;相反,引发异常,或只返回None
- 这样,调用代码可以决定如何处理错误和异常。
只要输出数量有限,你剩下的subprocess.Popen()
才会巧合。您应该使用subprocess.call
,它恰好用于在受控条件下运行子进程同时检查错误。
这里的关键观察是Popen()
本身只是产生子进程。你需要与它和wait()
进行交互,以确保它成功并返回其所有输出。 call
中的check_*
和各种subprocess
module方法为你做这件事; Popen()
主要用于你长大的罐装包装设备,但也有点更难以正确,特别是第一次。
tshark
命令不需要shell=True
,如果你自己将它分成一个列表,并进行排序并用Python写入文件。如果输出文件的唯一目的是从Python再次打开它,我建议将原始输出读入Python字符串并在Python中执行所有剩余的处理。
def get_all_tshark_out(logger, tcpdump, port):
output = subprocess.check_output(['tshark', '-r', str(tcpdump),
'-odiameter.tcp.ports:{0}'.format(port), '-R',
'diameter.cmd.code == 272 and diameter.flags.request==0 '
'and !tcp.analysis.retransmission and diameter.flags.T == 0',
'-Tpdml', '-Tfields', '-ediameter.Session-Id'])
return sorted(output)
...现在你的get_uniq_sessions
功能基本上是一个单行:
session = [x for x in get_all_tshark_out() if '1234' in x]