Python 测试网站是否能访问

今天有个需求,需要测试网站是否能访问,领导给了300个站点。

对此测试了下。- - # 写了个小工具。把结果中得成功访问和失败访问分开。多线程的跑。。

#coding:utf-8

#########################
#    Data:2014-5-17
#    Time:14:48
#    author:SmartTang
#########################

from Class.Threadpool_Class import Threadpool_Class
import httplib

result_list=[]
black_list=[]

def testping(url):
    conn=httplib.HTTPConnection(url)
    conn.request("GET","/")
    r1=conn.getresponse()
    if r1.status==200:
        result_list.append(url)
        print "存在地址: %s " % url
    else:
        black_list.append(url)
        print "存在异常的地址: %s " % url

obj=open('text.tasklist','r')
t_obj=Threadpool_Class()
for line in obj:
    _domain=line.strip('\n')
    if _domain.endswith('37.com'):
        t_obj.execute(testping,_domain)
obj.close()
t_obj.dismiss(True)

print "一共有: %d 个地址正常访问,%d 个地址无法正常访问." % (len(result_list),len(black_list))

print "写入到结果文件中:web_access.text"
ac_result=open("web_access.text","w+")
for r_item in result_list:
    ac_result.write(r_item+"\n")
ac_result.close()

print "写入到结果文件中:web_error.text"
er_result=open("web_error.text","w+")
for r_item in black_list:
    er_result.write(r_item+"\n")
er_result.close()

print "Work Finish!!"

 

posted @ 2014-05-17 15:35  墨迹哥's  阅读(3463)  评论(0编辑  收藏  举报