简体   繁体   中英

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?

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.

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.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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