0

I am trying to do a script that is hosting my file on my local network, here's my code :

import os
import getpass

os.system('python -m http.server --directory C:/Users/'+getpass.getuser())

But the probleme is that the http console is showing on my Desktop and that's annoying ! so I tried to hide by renaming the file in .pyw but it's not working.

Have you any idea on how to hide this console ? Thank you :D

4

3 回答 3

2

在 Linux 上,您可以使用Nohup忽略 HUP 信号。

您可以像这样在代码中添加 nohup:

import os
import getpass

os.system('nohup python -m http.server --directory C:/Users/'+getpass.getuser())

更新

窗户解决方案

import os
import subprocess
import getpass

env = os.environ
directory = 'C:/Users/'+getpass.getuser()
proc = subprocess.Popen(['python', '-m', 'http.server', '--directory', directory], env=env)
于 2021-11-26T12:45:05.327 回答
1

Assuming you're on Linux (or other unix based OS), you can detach the process from the console after starting the server.

Here is one way to do it with screen command

sudo apt install -Y screen

And then

screen -d -m "python3 script.py"

Where script.py is the snippet you have shared.

Reference for the flags

...

-d -m

Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.

于 2021-11-26T12:37:21.717 回答
0

我找到了一种方法,使用 VBS 脚本:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "main.py" & Chr(34), 0
Set WshShell = Nothing

只需替换main.py为脚本的路径即可。

于 2021-11-26T18:23:41.443 回答