2

Possible Duplicate:
Measuring ping latency of a server - Python

There are a few servers such as

http://server1.stackoverflow.com
http://server2.stackoverflow.com
http://server3.stackoverflow.com
http://server4.stackoverflow.com

and I want to know which links' response time is shortest at the moment. What should I do?

I am thinking about: timeit or ping the server.

I will be grateful if you could give me a sample or any idea, thank you. :)

Community
  • 1
  • 1
Anthony Lee
  • 59
  • 1
  • 2
  • 6

3 Answers3

1

Why do you want to do this yourself? Usually app servers are running behind a load balancer that hopefully redirects your requests to the machine with the least amount of load...load balancing in this context- done by yourself - makes perhaps sense in a geographically distributed app server setup but not here with Stackoverflow servers. So your usecase is?

See also

Measuring ping latency of a server - Python

Community
  • 1
  • 1
0

I am trying to google it for a half day, something like this?

url = ""

f = urllib.urlopen(url)
start = time.time()
page = f.read()
end = time.time()
f.close()

any better way?

Anthony Lee
  • 59
  • 1
  • 2
  • 6
0

you can use popen to execute ping command:

for example let's take the three nameservers of stackoverflow and ping them. Fastest one will be the one with lowest average time

import os
lis3=['64.34.119.33','64.34.119.34','69.59.196.217'] #nameservers of SO
lis2=[]
for x in lis3:
    strs=os.popen("ping "+x).read()
    #print strs
    time=strs[strs.rfind(' ')+1:strs.rfind('ms')]
    lis2.append(int(time))

minn=lis3[lis2.index(min(lis2))]
print 'lowest average time is from {0}'.format(minn)

output:

lowest average time is from 69.59.196.217
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504