目录
hyperscan安装:
其他依赖:
安装详情过程:
Hyperscan for python安装:
python hyperscan使用:
完整Demo代码:
hyperscan官方 Home Page - Hyperscan.io
hyperscan Github https://github.com/intel/hyperscan
hyperscan Source代码 Downloads - Hyperscan.io 或者可以去Github-Release里面去下载也可以
CMake CMake
Ragel Ragel State Machine Compiler
Python Welcome to Python.org Python一般默认Linux安装
Boots Boost C++ Libraries
Pcap TCPDUMP/LIBPCAP public repository 可不用安装,仅测试的时候会用到。
本来想详细写一下安装过程,但是看到有作者写过了,写的也非常详细,此处不再重复写。
文档:Getting Started — Hyperscan 5.4.0 documentation
看不太懂的可以看这个 CSDN: Hyperscan 安装_耿小渣的进阶之路-CSDN博客_hyperscan安装
pip安装
pip install hyperscan
源码安装。Poetry Poetry - Python dependency management and packaging made easy
$ pip install poetry
$ git clone https://github.com/darvid/python-hyperscan.git
$ cd python-hyperscan
$ poetry install
如果可以 import hyperscan 即安装成功。
以上两个安装也可参考官方的QuickStart Hyperscan for Python
可以参考官方Usage Usage - Hyperscan for Python
通常我们需要先创建db-obj即Building a database
import hyperscan
db = hyperscan.Database()
patterns = (
# expression, id, flags
(br'fo+', 0, 0),
(br'^foobar$', 1, hyperscan.HS_FLAG_CASELESS),
(br'BAR', 2, hyperscan.HS_FLAG_CASELESS
| hyperscan.HS_FLAG_SOM_LEFTMOST),
)
expressions, ids, flags = zip(*patterns)
db.compile(
expressions=expressions, ids=ids, elements=len(patterns), flags=flags
)
print(db.info().decode())
# Version: 5.1.1 Features: AVX2 Mode: BLOCK
然后Handling - Template
# Type annotated Hyperscan match handler signature
def on_match(
id: int,
from: int,
to: int,
flags: int,
context: Optional[Any] = None
) -> Optional[bool]:
...
Handling - Example (BlockMode)
def hyperscan_match(lines: List[str]) -> List[str]:
result = []
def on_match(id: int, froms: int, to: int, flags: int, context: Optional[Any] = None) -> Optional[bool]:
result.append(TAG_LOOKUP[id])
for line in lines:
db.scan(line.encode("utf-8"), match_event_handler=on_match)
return result
然后选择合适的Mode进行scan
Mode请参考上方的官方Usage
不再重复写到blog,前去 https://blog.gcoperation.top/posts/da2ec18d/ 查看。