读取配置文件,对每一个url进行检查,并行

 1 #coding:utf8
 2 '''
 3  题目三:
 4  有配置文件有类似下面的配置:
 5 http://192.168.41.55/img/9981|11|200
 6 http://192.168.41.55/img/9982|11|200
 7 http://192.168.41.55/img/9983|11|200
 8 http://192.168.41.55/img/9984|11|200
 9 http://192.168.41.55/img/9985|11|200
10  
11  编写一个脚本,要求如下:
12  1. 读取配置文件,对每一个url进行检查.
13     例如http://192.168.41.55/img/9985|11|200, 这个url返回的结果应该包含"11"两个字,并且HTTP状态码是200, 否则报错。
14  2. 需支持并行的检查多个链接 
15 '''
16 import re, urllib2
17 import threading
18 import time
19 index=0
20 rlock=threading.RLock()
21 class ExeThread(threading.Thread):
22     def __init__(self, path):
23         self.path=path
24         super(ExeThread, self).__init__()
25     def run(self):
26         #同步锁
27         rlock.acquire()
28         global index
29         temp=open(self.path)
30         temp.seek(index)
31         for i in range(100):
32             cnt=temp.readline()
33             exee(cnt)
34             index+=len(cnt)+1
35         #同步锁
36         rlock.release()
37 def exee(cnt):
38     if len(cnt)==0:
39         return
40     url, tag, code=cnt.split('|')
41     a=urllib2.urlopen(url)
42     content=a.read()
43     if tag in content and int(code)==a.code:
44         pass
45     else:
46         print '%serror'%cnt
47 path='f:/temp/html.txt'
48 for i in range(100):
49     exec('th=ExeThread(path);th.start()')
50   

 

posted @ 2013-04-19 10:33  hhhyde  阅读(273)  评论(0编辑  收藏  举报