首页 > 解决方案 > Python sort links by latency

问题描述

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

标签: pythonpython-requests

解决方案


一种方法是使用请求获取链接,然后测量经过的时间。例如:

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

文档。

注意:这不会测量整个页面的加载时间。例如,如果网站播放视频,则页面中不会包含实际视频,只有一个 HTML 元素告诉浏览器在哪里可以找到它。


推荐阅读