你想通了第1个参数,这是一个句柄到一个开放的事件日志。
根据[MS.Docs]:NotifyChangeEventLog函数 ( win32evtlog.NotifyChangeEventLog包装):
hEvent
手动重置或自动重置事件对象的句柄。 使用CreateEvent函数创建事件对象。
因此,您需要这样的东西。
code.py :
#!/usr/bin/env python3
import sys
import win32evtlog
import win32event
import win32api
import win32con
import msvcrt
def main():
server = None # "localhost" # name of the target computer to get event logs
source_type = "System" # "Application" # "Security"
h_log = win32evtlog.OpenEventLog(server, source_type)
flags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
total = win32evtlog.GetNumberOfEventLogRecords(h_log)
print(total)
h_evt = win32event.CreateEvent(None, 1, 0, "evt0")
win32evtlog.NotifyChangeEventLog(h_log, h_evt)
print("Waiting for changes in the '{:s}' event log. Press a key to exit...".format(source_type))
while not msvcrt.kbhit():
wait_result = win32event.WaitForSingleObject(h_evt, 500)
if wait_result == win32con.WAIT_OBJECT_0:
print("The '{:s}' event log has been modified".format(source_type))
# Any processing goes here
elif wait_result == win32con.WAIT_ABANDONED:
print("Abandoned")
win32api.CloseHandle(h_evt)
win32evtlog.CloseEventLog(h_log)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
注意事项 :
出于演示目的,我使用“ 系统 ”事件日志,因为有一种生成事件的简单方法
转到“ 服务 ”,选择一项(最好是未运行的一项),然后更改其“ 启动类型 ”。 单击“ 应用 ”时,将生成一个事件,该事件又将从脚本生成输出。
最后,别忘了撤消更改
输出 :
(py27x64_test) e:\\Work\\Dev\\StackOverflow\\q051036392>"e:\\Work\\Dev\\VEnvs\\py27x64_test\\Scripts\\python.exe" code.py Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] on win32 3430 Waiting for changes in the 'System' event log. Press a key to exit... The 'System' event log has been modified The 'System' event log has been modified