I want to measure time in miliseconds, that this line took:
before=datetime.datetime.now()
response = urllib2.urlopen("https://www.google.com")
after=datetime.datetime.now()
It is supposed to be kind of workaround for server, which doesn’t ping back, so I have to measure it from the server response.
I can get the string back string 0:00:00.034225 if I deduct two times and I am able to grab miliseconds as a substring, but I would like to get miliseconds in some cleaner way (whole difference in ms, including time converted from seconds, if the server responds with really big delay).
after - beforeis adatetime.timedeltaobject whosetotal_secondsmethod will give you what you are looking for. You can find additional information in the Python docs.You will just have to multiply by 1000 to get milliseconds. Don’t worry, although the method is called
total_seconds, it includes milliseconds as decimal places. Sample output:This won’t give you a timeout though, only a mesurement of the duration.