0

Attaching the snippet of my code.

Objective: I am trying to ping a website for certain interval of time (User defined). If the URL works, it will open the webpage in the default browser. If not it will keep pinging until the user defined time interval expires.

Issue: Code works perfectly but at instruction

response = os.system("ping " + url)

it pops up the cmd window on screen and I need to minimize it every time. It is irritating to do it manually all the time. Is there any way I can resize the terminal window or make it minimize all the time until the code expires?

A little manual study of OS lands me to os.get_terminal_size() but nothing much. I also looked into some other libraries that can resolve the issue but does not get any information

import os, time, webbrowser

#Enter URL that need to check. Add input()
url = "167.6.2.200"
input_time = float(input("How long ping should work: "))

current_time = time.time()   #note current time
actual_time = 0
isHostOn=0

#r = (os.get_terminal_size())
response =0 

#Execute the loop till the time expires entered by User
while((input_time + current_time) > actual_time): 
    response = os.system("ping " + url)
    if response ==0:
        webbrowser.open_new_tab("https://167.6.2.200:446/")
        break;
    else:
        #add Exception if any
        #print("no")
        pass # do nothing

    actual_time = time.time()
    #time.sleep(5)

Is there any possibility that I can resize/minimize the cmd window open everytime? Any change to execute the command in background.

P.S: I am using Window 10 OS

Shaswat Dube
  • 93
  • 1
  • 4
  • 2
    Possible duplicate of [How do I hide the console when I use os.system() or subprocess.call()?](https://stackoverflow.com/questions/7006238/how-do-i-hide-the-console-when-i-use-os-system-or-subprocess-call) – Lore Oct 03 '19 at 05:37
  • 1
    @Lore Got to know about the subprocess and libraries. Thanks for sharing :) – Shaswat Dube Oct 03 '19 at 07:39

1 Answers1

2

(With Windows 10)

Consider using the subprocess module, like so:

>>> import os, subprocess
>>> startupinfo = subprocess.STARTUPINFO()
>>> startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
>>> with subprocess.Popen("ping 192.168.1.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
    for line in p.stdout:
        print(line, end="")



Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
>>> with subprocess.Popen("ping 10.1.10.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
    for line in p.stdout:
        print(line, end="")



Pinging 10.1.10.1 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 10.1.10.1:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Iterate over p.stdout to get each line of the results as a string. Obviously you can ignore this if you want, in which case the text and stdout arguments can be removed.

Use p.returncode to get the return code.


On my Windows 10 machine, ping returns 0 if it receives a reply, and 1 if there's an error. Note that it returns 0 when I receive a Destination host unreachable response after trying to ping an address on my LAN that doesn't exist. Though I doubt it'll be an issue for your use case.

p.returncode will return None if you access it before the process has ended.

You can wait for the process to end by calling the p.wait() method, like so:

with subprocess.Popen("ping 192.168.1.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
    p.wait()
    print("Return code for 192.168.1.1:", p.returncode)
    for line in p.stdout:
        print(line, end="") # This will now all print immediately, after waiting for the process to finish

Output:

Return code for 192.168.1.1: 0

Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Or you can access p.returncode after the with statement, like so:

with subprocess.Popen("ping 10.1.10.1", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
    for line in p.stdout:
        print(line, end="")
print("Return code for 10.1.10.1:", p.returncode)

Output:

Pinging 10.1.10.1 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 10.1.10.1:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
Return code for 10.1.10.1: 1

Pinging a non-existant address on my LAN returns 0:

with subprocess.Popen("ping 192.168.1.99", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
    for line in p.stdout:
        print(line, end="")
print("Return code for 192.168.1.99:", p.returncode)

Output:

Pinging 192.168.1.99 with 32 bytes of data:
Reply from 192.168.1.105: Destination host unreachable.
Reply from 192.168.1.105: Destination host unreachable.
Reply from 192.168.1.105: Destination host unreachable.
Reply from 192.168.1.105: Destination host unreachable.

Ping statistics for 192.168.1.99:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Return code for 192.168.1.99: 0

Pinging an invalid URL returns 1:

with subprocess.Popen("ping www..com", startupinfo=startupinfo, text=True, stdout=subprocess.PIPE) as p:
    for line in p.stdout:
        print(line, end="")
print("Return code for www..com:", p.returncode)

Output:

Ping request could not find host www..com. Please check the name and try again.
Return code for www..com: 1
GordonAitchJay
  • 4,640
  • 1
  • 14
  • 16
  • It helps. I was little struggling with the return values of the subprocess.Popen but I understand it now. – Shaswat Dube Oct 03 '19 at 07:43
  • I've updated my answer. In short, the return code is only set once the process ends. So access `p.returncode` after the `with` statement, or after calling `p.wait()`. – GordonAitchJay Oct 04 '19 at 05:38
  • Thanks for adding the details. I already investigated on [https://stackoverflow.com/questions/5631624/how-to-get-exit-code-when-using-python-subprocess-communicate-method](how to get exit code when using python subprocess communicate method) and got to learn about it. – Shaswat Dube Oct 04 '19 at 08:53