美文网首页
用自带的python库去下载文件,超时后停止下载

用自带的python库去下载文件,超时后停止下载

作者: llicety | 来源:发表于2017-12-20 15:42 被阅读0次
        coding:utf-8
        import urllib2
        import multiprocessing
        import threading
        import os
        import signal
        from threading import Timer
    
        def download_file(url, fpath, timeout, msg_collector):
    
            def timeout_callback(dt):
                dt.terminate()
    
            msg_q = multiprocessing.Queue()
            dt = multiprocessing.Process(target=download_func, args=(url, fpath, msg_q))
            timer = threading.Timer(timeout, timeout_callback, [dt])
            dt.daemon = True
            dt.start()
            timer.start()
            dt.join()
            timer.cancel()
            msg = 'download timeout'
            try:
                msg = msg_q.get_nowait()
            except Exception as error:
                pass
            msg_collector['message'] = msg
            if msg == '200':
                return True
            else:
                return False
    
        def download_func(url, filename, msg_q):
            try:
                f = urllib2.urlopen(url)
                if f.code == 200:
                    with open(filename, 'wb') as tgfile:
                        tgfile.write(f.read())
                f.close()
            except Exception as error:
                print str(error)
                msg_q.put_nowait(str(error))
            else:
                msg_q.put_nowait(str(f.code))
    

    相关文章

      网友评论

          本文标题:用自带的python库去下载文件,超时后停止下载

          本文链接:https://www.haomeiwen.com/subject/nlbgwxtx.html