虽然当我写完的时候我发现wget可以一句话解决这个事情,不过还是与大家分享下代码吧~
更简单的方法详见:http://zqhong.qiniudn.com/2014/10/18/%E6%90%AD%E5%BB%BAOpenWrt%E6%9C%AC%E5%9C%B0%E8%BD%AF%E4%BB%B6%E5%BA%93%E9%95%9C%E5%83%8F/
下面是我写的小爬虫,是在其他人的成果基础上改的,新增支持级联目录
#!/usr/bin/env python #coding=utf-8 # # Openwrt Package Grabber # # Copyright (C) 2015 http://boweihe.me # based on http://shuyz.com # import urllib2 import os import re def fetch_package(url, subdir=''): print 'fetching package list from ' + url content = urllib2.urlopen(url, timeout=15).read() pattern = r'<a href="(.*?)">' items = re.findall(pattern, content) for item in items: if item=='../': continue else: m = re.match(r'\w+/$', item) if m: if not os.path.exists(savedir + subdir + m.group()): print 'makedir ' + m.group() os.makedirs(savedir + subdir + m.group()) fetch_package(url + m.group(), subdir + m.group()) else: print 'downloading item : '+ item rfile = urllib2.urlopen(url + item) opendir = savedir + subdir + item with open(opendir, 'wb') as code: code.write(rfile.read()) # the url of package list page, end with "/" baseurl = 'http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/' # which directory to save all the packages, end with "/" savedir = './download-full1407/' if not os.path.exists(savedir): os.makedirs(savedir) fetch_package(baseurl) print 'done!'
2 replies on “搭建自己的openwrt小镜像服务器”
[…] http://boweihe.me/?p=1537 […]
[…] 仅限Snapshot版本r48648使用,需要自己搭服务器的可以参考我的帖子http://boweihe.me/?p=1537 […]