【问题标题】:Minimizing the CMD window during code execution在代码执行期间最小化 CMD 窗口
【发布时间】:2020-02-01 09:41:05
【问题描述】:

附上我的代码的 sn-p。

目标:我正在尝试在某个时间间隔内 ping 一个网站(用户定义)。如果 URL 有效,它将在默认浏览器中打开网页。如果不是,它将一直 ping 直到用户定义的时间间隔到期。

问题:代码完美运行,但在指令中

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

它会在屏幕上弹出 cmd 窗口,我每次都需要将其最小化。一直手动操作很烦人。有什么办法可以调整终端窗口的大小或使其一直最小化,直到代码过期?

对操作系统的一点手动研究让我找到了 os.get_terminal_size() 但没什么。我还查看了其他一些可以解决问题但没有得到任何信息的库

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)

有没有可能我可以调整/最小化每次打开的 cmd 窗口?在后台执行命令的任何更改。

P.S:我使用的是 Window 10 操作系统

【问题讨论】:

标签: python


【解决方案1】:

(使用 Windows 10)

考虑使用subprocess 模块,如下所示:

>>> 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),

遍历p.stdout 以获取每行结果作为字符串。显然,如果您愿意,您可以忽略这一点,在这种情况下,可以删除 textstdout 参数。

使用p.returncode获取返回码。


在我的 Windows 10 机器上,ping 在收到回复时返回 0,如果出现错误则返回 1。请注意,当我在尝试 ping 我的 LAN 上不存在的地址后收到 Destination host unreachable 响应时,它会返回 0。虽然我怀疑这对您的用例来说是个问题。

p.returncode 将返回 None 如果您在进程结束之前访问它。

您可以通过调用p.wait() 方法等待进程结束,如下所示:

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

输出:

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

或者您可以在with 语句之后访问p.returncode,如下所示:

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)

输出:

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

在我的 LAN 上 Ping 一个不存在的地址返回 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)

输出:

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

ping 一个无效的 URL 返回 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)

输出:

Ping request could not find host www..com. Please check the name and try again.
Return code for www..com: 1

【讨论】:

  • 它有帮助。我对 subprocess.Popen 的返回值有点挣扎,但我现在明白了。
  • 我已经更新了我的答案。简而言之,只有在流程结束时才会设置返回码。所以在with 语句之后,或者在调用p.wait() 之后访问p.returncode
  • 感谢您添加详细信息。我已经对 [stackoverflow.com/questions/5631624/… 使用 python 子进程通信方法时获取退出代码进行了调查)并开始了解它。
猜你喜欢
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2021-09-30
  • 2011-07-28
相关资源
最近更新 更多