简体   繁体   English

ping 过程中时间不更新?

[英]Time doesn't update during ping process?

How to create a time on python when a process during ping starts like 01:25:25 and finishes like 01:25:39?当 ping 期间的进程在 01:25:25 开始并在 01:25:39 结束时,如何在 python 上创建时间?

I tried to do datetime input but not working just even the seconds still stays the same time as it started when finished.我尝试进行日期时间输入,但即使秒数仍然与完成后开始的时间相同,但无法正常工作。

my code我的代码

import os
from datetime import datetime

now = datetime.now()
time = now.strftime("%H:%M:%S")

pinghost = input("you want to ping: ")
print("Started " + time)
response = os.system("ping -n 6 {}".format(pinghost))

if response == 0:
    print('ping complete.')
    print("Completed "+time)
else:
    print('ping fail.')

You need to update your time variable whenever you're going to use it.每当您要使用它时,您都需要更新您的时间变量。 now would mean the present, when the command is running. now表示命令运行时的现在。

Just use:只需使用:

import os
from datetime import datetime

pinghost = input("you want to ping: ")
print("Started " + datetime.now().strftime("%H:%M:%S"))
response = os.system("ping -n 6 {}".format(pinghost))

if response == 0:
    print('ping complete.')
    print("Completed "+datetime.now().strftime("%H:%M:%S"))
else:
    print('ping fail.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM