-1

I have a list of links for eg: ['google.com','facebook.com','instagram.com']

I wanna sort the links by their latency. Is there any way i can ping the website and check latency?

If it's a video playing link then is it possible to test the speed?

Edit: Nick's answer solves the latency for me but still looking for an answer for speed of the server

sahil
  • 15
  • 4
  • 1
    Does this answer your question? [Pinging servers in Python](https://stackoverflow.com/questions/2953462/pinging-servers-in-python) – Yoshikage Kira May 18 '21 at 22:22
  • You can check the ping times by running `ping` and parsing the output. That doesn't say much about latency. In other words, you're not going to learn very much. There are a LOT of variables in ping time. You can learn your total network load, but to get individual web sites, you need a packet filter. That's advanced. – Tim Roberts May 18 '21 at 22:23
  • this question needs more focus, it's too broad – gold_cy May 18 '21 at 22:28

1 Answers1

1

One approach would be to fetch the link using requests, then measure the time elapsed. E.g:

for link in links:
    duration = requests.get(link).elapsed.total_seconds()
    print(link, duration)

Documentation.

Note: this won't measure the loading time of the entire page. For example, if the site plays a video, then the actual video won't be included in the page, only an HTML element which tells the browser where to find it.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • This works for me. But is there any way to test the speed of the website (at what speed will the video load)? – sahil May 18 '21 at 22:56
  • @sahil Yes, but it's much more complicated. You'd need to run a browser using something like [puppeteer](https://github.com/puppeteer/puppeteer). Then, you'd need to detect when the video is loaded. Doing that for a single site would be pretty easy. Doing it in a fully general way would be pretty hard. – Nick ODell Jun 15 '21 at 16:53