win 后台启动 django python manage.py runserver


win 后台启动 django;目的是以开发模式启动django,但是不显示cmd窗口,以避免被误关

新建start.py 启动该文件即可,将其配置到开机自启中

# -*- coding: utf-8 -*-



import threading 

import subprocess

import time



import win32api, win32gui



def exec_(command, cwd):

    """

    执行本地命令

    :param command: 命令

    :param cwd: 执行环境

    :return:

    """

    sub = subprocess.Popen(command, cwd=cwd, env=None, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

    # (stdout, stderr) = sub.communicate()

    # return stdout, stderr



def perform_task():

    """ 执行任务 """

    command = r'python manage.py runserver 0.0.0.0:80'

    cmdpath = r'D:\agent-bookkeeping-2\demo_app'



    exec_(command, cmdpath)





if __name__ == '__main__':

    """ 主方法入口 """

    ct = win32api.GetConsoleTitle()

    hd = win32gui.FindWindow(0,ct)

    win32gui.ShowWindow(hd,0)



    task = threading.Thread(target=perform_task, args=())

    task.start()



    print("over")