Home > Software engineering >  python http server hide console
python http server hide console

Time:11-26

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

CodePudding user response:

On Linux you can use Nohup to ignore the HUP signal.

You could add nohup on your code like this:

import os
import getpass

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

Update

Solution for windows

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)

CodePudding user response:

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.

  • Related