-1

I have this code to check ping to a server :

PingServer = subprocess.call('ping /n 2 /w 1000 ' + SERVER_IP +'')

When converted to EXE, this line outputs to the end user the command window with ping results which closes after 2 seconds.

Is there a way to hide it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TheWiz
  • 41
  • 4
  • 1
    maybe use `subprocess.get_output()` the same way, also for the exe convert it to noconsole – Matiiss Aug 12 '21 at 09:41
  • [Does this answer your question?](https://stackoverflow.com/q/764631/15740324) – Standard_101 Aug 12 '21 at 09:43
  • Executing external programs is rarely a good solution, if you can avoid it. And you can avoid it - use a package such as [pythonping](https://pypi.org/project/pythonping/) or [python-ping](https://pypi.org/project/python-ping/). Or you could roll your own ICMP utility code. – jarmod Aug 12 '21 at 21:47

1 Answers1

1

This is a serious answer to your question "this line outputs to the end user the command window with ping results which closes after 2 seconds. Is there a way to hide it?"

Yes there is, however a call to cmd is a call to run a console, thus a console by definition must be accessible and generally visible to the console user.

So the Option is given to minimise the in your face black screen, its a shame that the old ansi waiting... screen art died out with faster computers.

So replace your

PingServer = subprocess.call('ping /n 2 /w 1000 ' + SERVER_IP +'')

with something like

PingServer = subprocess.call('start "PingingServer" /min ping /n 2 /w 1000 ' + SERVER_IP +'', shell=True)

NOTE:- I have not tested how that would work in different applications "Calling" methods so the " quotes may need escaping in different ways

such as start \"PingingServer\" /min ... or start '+ TITLE_MSG +' /min ...

And a user would only see the brief notification hidden in the taskbar. Here ignore the background console that's me testing out this answer, I have intentionally expanded the task bar icon, to show the worst a user would see is that icon, and by the time they tried clicking the 2 second self destruct would have kicked in.

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36
  • Thank you - this looks like a great solution. Tried it manually in Windows CMD and it works great. But inside my code it is not working. Can you tell me what I`m missing here ? – TheWiz Aug 13 '21 at 08:39
  • It now tells me : FileNotFoundError: [WinError 2] The system cannot find the file specified. Is this because i removed the name of the window..? – TheWiz Aug 13 '21 at 08:46
  • 1
    It looks like it`s searching the START file. Maybe need slashes modifications? – TheWiz Aug 13 '21 at 08:52