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))
网友评论