class CodeUtil: def __init__(self, filePath): self.iniObj = IniUtil(filePath) #设置code节点 def codeStat(self): keyList = self.iniObj.getKeysBySection('result') for title in keyList: code_reason_Str = self.iniObj.getValue('result', title) codeStr = code_reason_Str.split(";")[0] code = codeStr.split(":")[1] self.setCode(code) #设置单个状态码数量 def setCode(self, code): self.iniObj.addSection("code") flag = self.iniObj.getValue("code", code) #状态码存在 if flag: num = int(flag) + 1 self.iniObj.setValue("code", code, str(num)) #状态码不存在,新增状态码key,初始值为1 else: self.iniObj.setValue("code", code, "1") #codeUtil = CodeUtil() #codeUtil.codeStat()
class CodeUtil: def __init__(self, filePath): self.iniObj = IniUtil(filePath) # 设置code节点 def codeStat(self): keyList = self.iniObj.getKeysBySection("result") for title in keyList: code_reason_Str = self.iniObj.getValue("result", title) codeStr = code_reason_Str.split(";")[0] code = codeStr.split(":")[1] self.setCode(code) # 设置单个状态码数量 def setCode(self, code): self.iniObj.addSection("code") flag = self.iniObj.getValue("code", code) # 状态码存在 if flag: num = int(flag) + 1 self.iniObj.setValue("code", code, str(num)) # 状态码不存在,新增状态码key,初始值为1 else: self.iniObj.setValue("code", code, "1")
cookieObj = CookieUtil() iniObj = IniUtil('../config/url.ini') cookie = cookieObj.getLoginCookie() #获取配置文件的url keysList = [] urlDict = {} keysList = iniObj.getKeysBySection('url') for key in keysList: pageCode = '' url = '' url = iniObj.getValue('url', key) urlDict.setdefault(key, url) #url = "http://kxyesit.cnsuning.com/" try: #创建请求的request req = urllib2.Request(url) handler = urllib2.HTTPCookieProcessor(cookie) #利用urllib2的build_opener方法创建一个opener openner = urllib2.build_opener(handler) responseCode = openner.open(req).getcode() #print '页面状态码为:' + str(responseCode) pageCode = u'状态码:' + str(responseCode) iniObj.setValue(section=u'状态码', key=key, value=pageCode) except urllib2.URLError, e: #print '错误原因:'+e.reason reason = e.reason pageCode = u'状态码:' + str(e.code) + u' 错误原因:' + reason #print pageCode iniObj.setValue(section=u'状态码', key=key, value=pageCode)
cookieObj = CookieUtil() iniObj = IniUtil('../config/url.ini') cookie = cookieObj.getLoginCookie() #获取配置文件的url keysList = [] urlDict = {} keysList = iniObj.getKeysBySection('url') for key in keysList: pageCode = '' url = '' url = iniObj.getValue('url',key) urlDict.setdefault(key,url) #url = "http://kxyesit.cnsuning.com/" try: #创建请求的request req = urllib2.Request(url) handler = urllib2.HTTPCookieProcessor(cookie) #利用urllib2的build_opener方法创建一个opener openner = urllib2.build_opener(handler) responseCode = openner.open(req).getcode() #print '页面状态码为:' + str(responseCode) pageCode = u'状态码:' + str(responseCode) iniObj.setValue(section=u'状态码',key=key,value=pageCode) except urllib2.URLError,e: #print '错误原因:'+e.reason reason = e.reason pageCode = u'状态码:' + str(e.code) + u' 错误原因:' + reason #print pageCode iniObj.setValue(section=u'状态码',key=key,value=pageCode)
class RunCode: def __init__(self): self.cookieObj = CookieUtil() self.nowTile = time.strftime('%Y%m%d%H%M%S') self.nowPath = os.getcwd() if os.path.exists(self.nowPath + "/../log") == False: os.mkdir(self.nowPath + "/../log") if os.path.exists(self.nowPath + "/../result") == False: os.mkdir(self.nowPath + "/../result") self.resultIniPath = self.nowPath + "/../log/result_" + self.nowTile + ".ini" self.htmlPath = self.nowPath + "/../result/result_" + self.nowTile + ".html" self.check = Check() self.logUtil = LogUtil() def run(self): checkKeyResult = self.check.checkIniKeys() #key重复性校验失败 if checkKeyResult: errorKeyPath = self.nowPath + "/../log/result_" + self.nowTile + "_error.ini" self.logUtil.setKeyLog(errorKeyPath, checkKeyResult) #校验成功 else: checkUrlResult = self.check.checkIniUrl() #url合法性校验失败 if checkUrlResult: errorUrlPath = self.nowPath + "/../log/result_" + self.nowTile + "_error.ini" self.logUtil.setUrlLog(errorUrlPath, checkUrlResult) #校验成功 else: self.iniObj = IniUtil(self.nowPath + '/../config/url.ini') self.iniObjResult = IniUtil(self.resultIniPath) cookie = self.getCookie() self.doPost(cookie) self.setCode() self.setHtml() print '执行完毕!!' def getCookie(self): cookie = self.cookieObj.getLoginCookie() return cookie def doPost(self, cookie): #获取配置文件的url keysList = [] urlDict = {} keysList = self.iniObj.getKeysBySection('url') for key in keysList: pageCode = '' url = '' url = self.iniObj.getValue('url', key) urlDict.setdefault(key, url) #url = "http://kxyesit.cnsuning.com/" try: #创建请求的request req = urllib2.Request(url) handler = urllib2.HTTPCookieProcessor(cookie) #利用urllib2的build_opener方法创建一个opener openner = urllib2.build_opener(handler) responseCode = openner.open(req).getcode() #print '页面状态码为:' + str(responseCode) pageCode = u'状态码:' + str(responseCode) #iniObj.setValue(section='result',key=key,value=pageCode) self.iniObjResult.setValue(section='result', key=key, value=pageCode) except urllib2.URLError, e: #print '错误原因:'+e.reason reason = e.reason pageCode = u'状态码:' + str(e.code) + u';错误原因:' + reason #print pageCode #iniObj.setValue(section='result',key=key,value=pageCode) self.iniObjResult.setValue(section='result', key=key, value=pageCode)
class RunCode: def __init__(self): self.cookieObj = CookieUtil() self.nowTile = time.strftime("%Y%m%d%H%M%S") self.nowPath = os.getcwd() if os.path.exists(self.nowPath + "/../log") == False: os.mkdir(self.nowPath + "/../log") if os.path.exists(self.nowPath + "/../result") == False: os.mkdir(self.nowPath + "/../result") self.resultIniPath = self.nowPath + "/../log/result_" + self.nowTile + ".ini" self.htmlPath = self.nowPath + "/../result/result_" + self.nowTile + ".html" self.check = Check() self.logUtil = LogUtil() def run(self): checkKeyResult = self.check.checkIniKeys() # key重复性校验失败 if checkKeyResult: errorKeyPath = self.nowPath + "/../log/result_" + self.nowTile + "_error.ini" self.logUtil.setKeyLog(errorKeyPath, checkKeyResult) # 校验成功 else: checkUrlResult = self.check.checkIniUrl() # url合法性校验失败 if checkUrlResult: errorUrlPath = self.nowPath + "/../log/result_" + self.nowTile + "_error.ini" self.logUtil.setUrlLog(errorUrlPath, checkUrlResult) # 校验成功 else: self.iniObj = IniUtil(self.nowPath + "/../config/url.ini") self.iniObjResult = IniUtil(self.resultIniPath) cookie = self.getCookie() self.doPost(cookie) self.setCode() self.setHtml() print "执行完毕!!" def getCookie(self): cookie = self.cookieObj.getLoginCookie() return cookie def doPost(self, cookie): # 获取配置文件的url keysList = [] urlDict = {} keysList = self.iniObj.getKeysBySection("url") for key in keysList: pageCode = "" url = "" url = self.iniObj.getValue("url", key) urlDict.setdefault(key, url) # url = "http://kxyesit.cnsuning.com/" try: # 创建请求的request req = urllib2.Request(url) handler = urllib2.HTTPCookieProcessor(cookie) # 利用urllib2的build_opener方法创建一个opener openner = urllib2.build_opener(handler) responseCode = openner.open(req).getcode() # print '页面状态码为:' + str(responseCode) pageCode = u"状态码:" + str(responseCode) # iniObj.setValue(section='result',key=key,value=pageCode) self.iniObjResult.setValue(section="result", key=key, value=pageCode) except urllib2.URLError, e: # print '错误原因:'+e.reason reason = e.reason pageCode = u"状态码:" + str(e.code) + u";错误原因:" + reason # print pageCode # iniObj.setValue(section='result',key=key,value=pageCode) self.iniObjResult.setValue(section="result", key=key, value=pageCode)