resolv_code
2015-08-04 10:52
573 查看
#!/usr/bin/python # encoding: utf-8 import time,sys import urllib2,httplib import threading def ReadHost(file): hosts=[] f=open(file,'r') for line in f: li=line.strip('\n') hosts.append(line.strip()) f.close() return hosts class ThreadClass(threading.Thread): def __init__(self,host): threading.Thread.__init__(self) self.host=host def run(self): global CodeHost try: res=urllib2.urlopen(self.host).getcode() if mutex.acquire(1): CodeHost[self.host]=res mutex.release() except urllib2.HTTPError,e: CodeHost[self.host]=e.code except Exception,e: pass def MulThreadResolve(fr): start=time.ctime() print 'starting MulThreadResolve at: ',start hosts=ReadHost(fr) threads=[] for host in hosts: t=ThreadClass(host) threads.append(t) cntHost=len(hosts) #cntHost=3 for i in range(cntHost): threads[i].start() for i in range(cntHost): threads[i].join() print 'ending MulThreadResolve at :', time.ctime(),'\n' if __name__=='__main__': CodeHost={} mutex=threading.Lock() MulThreadResolve('img_dir.txt') print "\033[32m %-35s %-10s\033[0m" % ('url','code') for k,v in CodeHost.items(): if v==200: print "\033[32m %-35s %-10s\033[0m" % (k,v) if v==403: print "\033[33m %-35s %-10s\033[0m" % (k,v) if v==404: print "\033[31m %-35s %-10s\033[0m" % (k,v)
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法
- Python实现的基于ADB的Android远程工具