Created
July 2, 2013 16:52
-
-
Save harmy/5911025 to your computer and use it in GitHub Desktop.
使用七牛的镜像存储为网站加速,用这个脚本去把所有的资源请求一遍,效果立竿见影。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=gbk | |
import sys | |
import zipfile as zf | |
from urlparse import urlparse | |
from threading import Thread | |
import httplib, sys | |
from Queue import Queue | |
concurrent = 200 | |
q=Queue(concurrent*2) | |
def doWork(): | |
while True: | |
url = q.get() | |
status, url = getStatus(url) | |
doSomethingWithResult(status, url) | |
q.task_done() | |
def getStatus(ourl): | |
try: | |
url = urlparse(ourl) | |
conn = httplib.HTTPConnection(url.netloc) | |
conn.request("HEAD", url.path) | |
res = conn.getresponse() | |
return res.status, ourl | |
except: | |
return "error", ourl | |
def doSomethingWithResult(status, url): | |
print status, url | |
for i in range(concurrent): | |
t=Thread(target=doWork) | |
t.daemon=True | |
t.start() | |
try: | |
with zf.ZipFile('res.zip', 'r') as zipFile: | |
infolist = [info for info in zipFile.infolist() if info.CRC != 0] | |
for info in infolist: | |
q.put('http://ascq.qiniudn.com/update/{0:x}'.format(info.CRC)) | |
q.join() | |
except KeyboardInterrupt: | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment