附加我的代码段。
目标:我试图在一定时间间隔内对网站进行ping操作(用户定义)。如果该URL有效,它将在默认浏览器中打开该网页。如果没有,它将继续ping,直到用户定义的时间间隔到期为止。
问题:代码可正常运行,但需遵循指令
响应= os.system(“ ping” +网址)
它会在屏幕上弹出cmd窗口,我每次都需要将其最小化。一直手动进行很烦人。我有什么办法可以调整终端窗口的大小或使其最小化,直到代码到期?
对OS进行的一些手动研究使我进入了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:我正在使用Windows 10操作系统
答案 0 :(得分:2)
(对于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
,以字符串的形式获取结果的每一行。显然,您可以根据需要忽略此情况,在这种情况下,可以删除text
和stdout
参数。
使用p.returncode
获取返回码。
在我的Windows 10计算机上,ping
如果收到答复,则返回0,如果有错误,则返回1。请注意,在尝试对局域网上不存在的地址执行ping操作后收到目标主机不可达响应时,它返回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
或者您可以在p.returncode
语句之后访问with
,如下所示:
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
在我的局域网上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