Home  >  Q&A  >  body text

python - How to get download speed elegantly?

I used to use urlopen and then read, open a thread, and detect the received buffer size every second. Is there a more elegant way to gain speed?

天蓬老师天蓬老师2734 days ago733

reply all(5)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-18 10:46:51

    For urllib.urlretrieve(url[, filename[, reporthook[, data]]]), write the callback function

    import time
    import urllib
    start_time = time.time()
    def Schedule(a,b,c):
        '''
        a:已经下载的数据块
        b:数据块的大小
        c:远程文件的大小
        '''
        speed = (a * b) / (time.time() - start_time)
        print speed
        
    urllib.urlretrieve(url,local,Schedule)

    reply
    0
  • PHP中文网

    PHP中文网2017-05-18 10:46:51

    Use progreebar

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-18 10:46:51

    streaming.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:46:51

    Have you tried detecting the increment of the downloaded file?
    Check the local size of the downloaded file regularly and take the increment.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-18 10:46:51

    If it is python, most libraries will provide callback functions to do this work.

    reply
    0
  • Cancelreply