【发布时间】:2018-06-25 13:39:43
【问题描述】:
我目前正在尝试让一个 python 库在我的机器上运行,但由于某种原因,它的编写方式似乎被破坏了。
该库将两个 json 文件作为输入,其中一个名为 library.json,在 library.json 中,它会执行如下所示的多项检查:
def name(self, n):
if n and isinstance(n, str):
if re.search(r'[^a-zA-Z0-9_.]+', n):
raise ValueError("Name can only contain alphanumeric characters, underscores and periods. Name is: {}".format(n))
if re.match(r'^\d', n):
raise ValueError("Name cannot start with a number. The name is: {}".format(n))
self._name = n
else:
raise ValueError('Name must be a non-empty string')
我使用的是直接从文档中提取的简单 library.json:
{
"word_launching_powershell": {"datetime_field": "datetime",
"weight": 10,
"indicators": [{"field": "process", "value": "powershell.exe"},
{"field": "parent_process", "value": "winword.exe"}]
}
}
还有一个简单的测试文件,同样直接取自文档:
from electus import Electus
# Create some sample data
events = [{
"process": "winword.exe",
"parent_process": "explorer.exe",
"datetime": "2017-10-16T17:15:47.030114"
},
{"process": "powershell.exe",
"parent_process": "winword.exe",
"datetime": "2017-10-16T17:15:48.030123"
}]
# The library.json file stores the indicator definitions
# The combinations of indicators are defined in jobs.json
e = Electus(library_conf='library.json', job_conf='jobs.json')
for event in events:
alerts = e.evaluate_event(event)
if alerts:
print(alerts)
所以,当我运行这个测试脚本时,我得到以下信息:
Traceback (most recent call last):
File "test_config.py", line 16, in <module>
lib_def = LibraryDefinition(name='word_launching_powershell', definition=definition)
File "/home/user/.local/lib/python2.7/site-packages/electus/library.py", line 17, in __init__
self.name = name
File "/home/user/.local/lib/python2.7/site-packages/electus/library.py", line 47, in name
raise ValueError('Name must be a non-empty string')
ValueError: Name must be a non-empty string
做一些调试,我看到它在这里死了:
n = {unicode}u'word_launching_powershell'
所以这对我来说似乎是一个编码问题,我不明白的是为什么整个库都会用那些 isinstance 检查它们是否总是使用 json 文件失败?我有什么办法可以修复编码吗?
【问题讨论】:
标签: python json python-2.7