久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python執行cmd命令,怎么讓他執行類似Ctrl+C效果將其結束命令?

瀏覽:159日期:2022-08-10 08:37:52

問題描述

python執行cmd命令,怎么讓他執行Ctrl+C的效果結束命令?我在用ping監控一個服務器的網絡狀態,我執行了ping命令后,他會一直這么ping下去,不停止,怎么讓他在10秒后執行ctrl+c的效果

def re(cmd): while True:os.system(cmd);re('ping 192.168.1.1 -t')

他會這樣一直ping下去,想了半天也想不出怎么讓他10秒后執行ctrl+c結束的執行效果,請教大神,怎么讓他執行結束命令;10秒后停止命令,類似執行ctrl+c的效果;

問題解答

回答1:

用kill就可以了

回答2:

os.system 的控制力很弱的,你需要 subprocess 模塊的 .kill 方法來發送信號。

不過,只是 ping 的話,你干嘛不讓它只 ping 10 次呢?你這是想干嘛啊?

回答3:

[root@jenkins xxxx]# time ping baidu.com -w 10sPING baidu.com (220.181.57.217) 56(84) bytes of data.64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=1 ttl=52 time=4.07 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=2 ttl=52 time=26.9 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=3 ttl=52 time=6.78 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=4 ttl=52 time=12.9 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=5 ttl=52 time=3.86 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=6 ttl=52 time=3.30 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=7 ttl=52 time=5.63 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=8 ttl=52 time=11.2 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=9 ttl=52 time=4.30 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=10 ttl=52 time=4.17 ms--- baidu.com ping statistics ---10 packets transmitted, 10 received, 0% packet loss, time 9013msrtt min/avg/max/mdev = 3.300/8.328/26.972/6.954 msreal 0m10.006suser 0m0.003ssys 0m0.006s

不知道能不能滿足你的需求

回答4:

主要問題是執行的命令一直沒有返回,os.system無法確定命令是否已經執行和執行成功如果單純的cmd命令 是否可以試試os.system(’start ping www.baidu.com -t’)

回答5:

注冊signal.SIGINT信號

回答6:

我試了一下,大概可以這樣寫:

import timestart = end = time.time()def re(cmd): while (end - start) < 10:os.system(cmd);end = time.time()

標簽: Python 編程
主站蜘蛛池模板: 双辽市| 许昌市| 绥中县| 东阳市| 师宗县| 象山县| 府谷县| 体育| 雷州市| 福贡县| 桃江县| 凤山县| 清镇市| 民权县| 长泰县| 长岭县| 贵德县| 溆浦县| 靖州| 安阳县| 临邑县| 江阴市| 丹巴县| 门源| 云龙县| 盐亭县| 丰台区| 奈曼旗| 郑州市| 大安市| 正蓝旗| 伊宁市| 平利县| 丹棱县| 沛县| 麻栗坡县| 广水市| 中西区| 昌图县| 肇庆市| 弥渡县|