[英]How to *properly* compress and decompress a text file using bz2 and python
[英]How to decompress BZ2 directly into Popen stdin in Python 2.7?
场景:我想用tcpdump
解析用BZIP2
压缩的PCAP
文件,并在Python 2.7中逐行列出结果。 这是我想到的:
def tcpdump(filename):
import subprocess
import bz2
p = subprocess.Popen(
('tcpdump', '-lnr', '-s', '0', '-'),
stdin=bz2.BZ2File(filename),
stdout=subprocess.PIPE)
try:
for row in p.stdout:
yield row.rstrip()
except KeyboardInterrupt:
p.terminate()
这个问题是Popen
的stdin
参数需要一个实际的文件句柄并引发此异常:
AttributeError:'bz2.BZ2File'对象没有属性'fileno'
我可以轻松地将其分为两步,但是我想避免使用中间临时文件。
有什么想法或建议吗?
使用两个不同的Popen对象:
p1 = subprocess.Popen(['bunzip2', '-c', filename],
stdout=subprocess.PIPE)
p2 = subprocess.Popen(['tcpdump', '-lnr', '-s', '0', '-'],
stdin=p1.stdout,
stdout=subprocess.PIPE)
p1.stdout.close()
for row in iter(p2.stdout.readline, b''):
...
为了避免bunzip2
依赖性,您可以手动泵送输入:
import subprocess
import threading
from contextlib import closing
p = subprocess.Popen(['tcpdump', '-lnr', '-s', '0', '-'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=-1)
threading.Thread(target=pump, args=[filename, p.stdin]).start()
with closing(p.stdout):
for line in iter(p.stdout.readline, b''):
print line,
p.wait()
其中pump()
是:
from shutil import copyfileobj
def pump(filename, pipe):
"""Decompress *filename* and write it to *pipe*."""
with closing(pipe), bz2.BZ2File(filename) as input_file:
copyfileobj(input_file, pipe)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.