微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 脚本不会在 Windows 的后台启动

如何解决Python 脚本不会在 Windows 的后台启动

所以我有这两个脚本:

import os
import subprocess
import sys

def path():
    osupathcheck = True
    while osupathcheck:
        osupath = input("enter osu path here: ")
        print(os.path.abspath(osupath + '/osu!.exe'))
        try:
            if os.path.isfile(os.path.abspath(osupath + '/osu!.exe')):
                print("Success: folder contain osu.exe")
                with open("path.txt","w") as f:
                    f.write(osupath)
                osupathcheck = False
            else:
                print("Error: Folder doesn't contain osu.exe")

        except NameError:
            print("Error: Input isn't a path or the specified path is incorrect")

def spawn_program_and_die(program,exit_code=0):
    subprocess.Popen(program)
    sys.exit(exit_code)

if __name__ == "__main__":
    print("Checking modules...")
    try: 
        import browser_cookie3
        import requests
        from bs4 import BeautifulSoup as BS
        print("successfully imported browser_cookie3,requests,bs4 and Flask")
    except ImportError:
        promptm = True
        while promptm:
            i = input("browser_cookie3,requests and bs4 are required to download maps from this program,would you like to install these packages? (Require pip) Y/n: ")
            if i == "Y" or i == "y":
                subprocess.call([sys.executable,"-m","pip","install","browser_cookie3"])
                subprocess.call([sys.executable,"requests"])
                subprocess.call([sys.executable,"bs4"])
                subprocess.call([sys.executable,"Flask"])
                print("successfully imported browser_cookie3,bs4 and Flask")
                promptm = False
            if i == "N" or i == "n":
                print("exiting...")
                exit()
    print("checking path")
    if not os.path.exists("./path.txt") or os.path.getsize("path.txt") == 0:
        path()
    spawn_program_and_die(['pythonw','AltDownload.py'])

和服务器脚本:

import browser_cookie3
import requests
from bs4 import BeautifulSoup as BS
from flask import Flask,request
import re
import os
import asyncio

async def Download_map(osupath,link):
    cj = browser_cookie3.load()
    print("Downloading",link)
    headers = {"referer": link}
    with requests.get(link) as r:
        t = BS(r.text,'html.parser').title.text.split("·")[0]
    with requests.get(link+"/download",stream=True,cookies=cj,headers=headers) as r:
        if r.status_code == 200:
            try:
                id = re.sub("[^0-9]","",link)
                with open(os.path.abspath(osupath+"/Songs/"+id+" "+t+".osz"),"wb") as otp:
                    otp.write(r.content)
            except:
                print("You either aren't connected on osu!'s website or you're limited by the API,in which case you Now have to wait 1h and then try again.")

async def Download_prep(link):
    
    print("checking link...")

    with open("path.txt",'r') as f:
        osupath = f.read()

    if link.split("/",4)[:4] == ['https:','','osu.ppy.sh','beatmapsets']:
        print("Downloading map...")
        asyncio.run(Download_map(osupath,link.split("#osu")[0]))

app = Flask(__name__)

@app.route('/')
def index():
    asyncio.run(Download_prep(request.args.get('data')))
    return "OK"

app.run(port=5000)

出于某种原因,当我尝试在后台运行它时,通过使用 pythonw 执行它,或者重命名文件 .pyw 或使用子进程,还有一种我忘记的方法,它不起作用,没有生成 python 进程或者它可能会因为我不知道的原因立即关闭。 使用 python 和 os.system() 正常执行脚本按预期工作并实际运行脚本。 我可以用什么来让这个服务器脚本在后台工作?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。