def getHtmlLabel(self): iniObj = IniUtil(self.configIniPath) sections = iniObj.getSections() list = [] for sec in sections: tmpList = iniObj.getKeysBySection(sec) list.extend(tmpList) return list
def __init__(self): self.fileName = '../config/cookie.txt' self.iniObj = IniUtil('../config/config.ini') self.loginUrl = self.iniObj.getValue('loginUrl', 'url') #self.loginUrl = 'https://passport.suning.com/ids/login?method=GET&loginTheme=b2c' self.vCodeName = "../config/vcode.png" #MozillaCookieJar提供可读写操作的cookie文件,存储cookie对象 self.cookiejar = cookielib.MozillaCookieJar(self.fileName)
def checkIniUrl(self): iniObj = IniUtil(self.iniPath) keys = iniObj.getKeysBySection('url') for key in keys: url = iniObj.getValue('url',key) if self.checkUrl(url) == False: self.falseUrl.append(url) return self.falseUrl
def checkIniUrl(self): iniObj = IniUtil(self.iniPath) keys = iniObj.getKeysBySection('url') for key in keys: url = iniObj.getValue('url', key) if self.checkUrl(url) == False: self.falseUrl.append(url) return self.falseUrl
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()
def __init__(self): self.fileName = '../config/cookie.txt' self.iniObj = IniUtil('../config/config.ini') self.loginUrl = self.iniObj.getValue('loginUrl','url') #self.loginUrl = 'https://passport.suning.com/ids/login?method=GET&loginTheme=b2c' self.vCodeName = "../config/vcode.png" #MozillaCookieJar提供可读写操作的cookie文件,存储cookie对象 self.cookiejar = cookielib.MozillaCookieJar(self.fileName)
def run(self): checkKeyResult = self.check.checkIniKeys() #key重复性校验失败 if checkKeyResult: self.logUtil.setKeyLog(self.errorLogPath, checkKeyResult) #校验成功 else: checkUrlResult = self.check.checkIniUrl() #url合法性校验失败 if checkUrlResult: self.logUtil.setUrlLog(self.errorLogPath, checkUrlResult) #校验成功 else: self.iniObj = IniUtil(self.nowPath + '/../config/url.ini') self.iniObjResult = IniUtil(self.resultIniPath) cookie = self.getCookie() #解析内容 self.getContent(cookie) #self.doPost(cookie) #self.setCode() #self.setHtml() print '执行完毕!!'
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 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 "执行完毕!!"
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")
def run(self): checkKeyResult = self.check.checkIniKeys() # key重复性校验失败 if checkKeyResult: self.logUtil.setKeyLog(self.errorLogPath, checkKeyResult) # 校验成功 else: checkUrlResult = self.check.checkIniUrl() # url合法性校验失败 if checkUrlResult: self.logUtil.setUrlLog(self.errorLogPath, checkUrlResult) # 校验成功 else: self.iniObj = IniUtil(self.nowPath + "/../config/url.ini") self.iniObjResult = IniUtil(self.resultIniPath) cookie = self.getCookie() # 解析内容 self.getContent(cookie) # self.doPost(cookie) # self.setCode() # self.setHtml() print "执行完毕!!"
def __init__(self, filePath): self.iniObj = IniUtil(filePath)
def __init__(self, resultIniPath, htmlPath): html = open(htmlPath, 'w+') #iniObj = IniUtil('../log/result.ini') iniObj = IniUtil(resultIniPath) strFirst = ''' <html> <head> <meta charset="utf-8" /> <title>状态码</title> </head> <body> <table align="center" width="80%" border="1"> <tbody> <tr> <td colspan="3" align="center"> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="codeImg" style="height:300px;width:400px;"></div> </td> </tr> <tr> <td colspan="3" align="center"> <table width="100%" border="1" cellspacing="0" cellpadding="0" class="sp-order-table"> <thead> <tr bgcolor="#CECEFF"> <td>标题</td> <td>URL</td> <td>状态码</td> <td>结果</td> </tr> </thead> ''' colorRed = "style='color:#FF0000'" colorGreen = "style='color:#00FF7F'" titleList = iniObj.getKeysBySection('result') strTbody = '<tbody>' codeStr = '' code = '' reasonStr = '' reason = '' colorFlag = '' for title in titleList: codeStr = '' reasonStr = '' code_reason_Str = iniObj.getValue('result', title) if ";" in code_reason_Str: codeStr = code_reason_Str.split(";")[0] #code = codeStr.split(":")[1] reasonStr = code_reason_Str.split(";")[1] #reason = reasonStr.split(":")[1] colorFlag = colorRed else: reasonStr = '通过' codeStr = code_reason_Str.split(";")[0] #code = codeStr.split(":")[1] colorFlag = colorGreen strTbody += "<tr>" strTbody += "<td>" + title + "</td>" strTbody += "<td>" + iniObj.getValue('result', title) + "</td>" strTbody += "<td>" + codeStr + "</td>" strTbody += "<td " + colorFlag + ">" + reasonStr + "</td>" strTbody += "</tr>" strTbody += '</tbody>' strSecond = ''' </table> </td> </tr> </tbody> </table> <!-- ECharts单文件引入 --> <script src="../config/echarts.common.min.js"></script> <style> body{ text-align:center} .sp-order-table { clear: both; border: 1px solid #ebebeb; margin-top: 20px; } </style> <script type="text/javascript"> // 基于准备好的dom,初始化echarts图表 var myChart = echarts.init(document.getElementById('codeImg')); var option = { //标题 title : { text: '状态码', //subtext: '纯属虚构', x:'center' }, ''' dataName = '[' data = '' strThird = '' codeList = iniObj.getKeysBySection('code') for codeName in codeList: dataName += "'" + codeName + "'," num = iniObj.getValue('code', codeName) data += "{value:" + num + ", name:'" + codeName + "'}," dataName = dataName[:-1] + ']' strThird = ''' //左侧竖直列 legend: { orient : 'vertical', x : 'left', data:''' + dataName + ''' }, //饼图值 series : [ { name:'状态码', type:'pie', radius : '55%', center: ['50%', '60%'], data:[''' + data[:-1] + ''' ] } ] }; ''' strFour = ''' // 为echarts对象加载数据 myChart.setOption(option); </script> </body> </html> ''' strHtml = strFirst + strTbody + strSecond + strThird + strFour html.write(strHtml) html.close()
# -*- coding:utf-8 -*- import urllib2 from iniUtil import IniUtil from cookieUtil import CookieUtil 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
# -*- coding:utf-8 -*- import urllib2 from iniUtil import IniUtil from cookieUtil import CookieUtil 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
def __init__(self,resultIniPath,htmlPath): html = open(htmlPath, 'w+') #iniObj = IniUtil('../log/result.ini') iniObj = IniUtil(resultIniPath) strFirst = ''' <html> <head> <meta charset="utf-8" /> <title>状态码</title> </head> <body> <table align="center" width="80%" border="1"> <tbody> <tr> <td colspan="3" align="center"> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="codeImg" style="height:300px;width:400px;"></div> </td> </tr> <tr> <td colspan="3" align="center"> <table width="100%" border="1" cellspacing="0" cellpadding="0" class="sp-order-table"> <thead> <tr bgcolor="#CECEFF"> <td>标题</td> <td>URL</td> <td>状态码</td> <td>结果</td> </tr> </thead> ''' colorRed = "style='color:#FF0000'" colorGreen = "style='color:#00FF7F'" titleList = iniObj.getKeysBySection('result') strTbody = '<tbody>' codeStr = '' code = '' reasonStr = '' reason = '' colorFlag = '' for title in titleList: codeStr = '' reasonStr = '' code_reason_Str = iniObj.getValue('result',title) if ";" in code_reason_Str: codeStr = code_reason_Str.split(";")[0] #code = codeStr.split(":")[1] reasonStr = code_reason_Str.split(";")[1] #reason = reasonStr.split(":")[1] colorFlag = colorRed else: reasonStr = '通过' codeStr = code_reason_Str.split(";")[0] #code = codeStr.split(":")[1] colorFlag = colorGreen strTbody += "<tr>" strTbody += "<td>"+title+"</td>" strTbody += "<td>"+iniObj.getValue('result',title)+"</td>" strTbody += "<td>"+codeStr+"</td>" strTbody += "<td "+colorFlag+">"+reasonStr+"</td>" strTbody += "</tr>" strTbody +='</tbody>' strSecond=''' </table> </td> </tr> </tbody> </table> <!-- ECharts单文件引入 --> <script src="../config/echarts.common.min.js"></script> <style> body{ text-align:center} .sp-order-table { clear: both; border: 1px solid #ebebeb; margin-top: 20px; } </style> <script type="text/javascript"> // 基于准备好的dom,初始化echarts图表 var myChart = echarts.init(document.getElementById('codeImg')); var option = { //标题 title : { text: '状态码', //subtext: '纯属虚构', x:'center' }, ''' dataName = '[' data = '' strThird = '' codeList = iniObj.getKeysBySection('code') for codeName in codeList: dataName += "'" + codeName + "'," num = iniObj.getValue('code',codeName) data += "{value:"+num+", name:'"+codeName+"'}," dataName = dataName[:-1] + ']' strThird = ''' //左侧竖直列 legend: { orient : 'vertical', x : 'left', data:'''+dataName+''' }, //饼图值 series : [ { name:'状态码', type:'pie', radius : '55%', center: ['50%', '60%'], data:['''+data[:-1]+''' ] } ] }; ''' strFour = ''' // 为echarts对象加载数据 myChart.setOption(option); </script> </body> </html> ''' strHtml = strFirst+strTbody+strSecond+strThird+strFour html.write(strHtml) html.close()
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.errorLogPath = self.nowPath + "/../log/result_" + self.nowTile + "_error.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: self.logUtil.setKeyLog(self.errorLogPath, checkKeyResult) # 校验成功 else: checkUrlResult = self.check.checkIniUrl() # url合法性校验失败 if checkUrlResult: self.logUtil.setUrlLog(self.errorLogPath, checkUrlResult) # 校验成功 else: self.iniObj = IniUtil(self.nowPath + "/../config/url.ini") self.iniObjResult = IniUtil(self.resultIniPath) cookie = self.getCookie() # 解析内容 self.getContent(cookie) # self.doPost(cookie) # self.setCode() # self.setHtml() print "执行完毕!!" def getCookie(self): cookie = self.cookieObj.getLoginCookie() return cookie def getContent(self, cookie): # 获取配置文件的url keysList = [] urlDict = {} keysList = self.iniObj.getKeysBySection("url") num = 1 excelUtilObj = ExcelUtil() for key in keysList: url = self.iniObj.getValue("url", key) urlDict.setdefault(key, url) try: # 创建请求的request req = urllib2.Request(url) handler = urllib2.HTTPCookieProcessor(cookie) # 利用urllib2的build_opener方法创建一个opener openner = urllib2.build_opener(handler) response = openner.open(req) content = response.read() htmlLabel = excelUtilObj.htmlLabel worksheet = excelUtilObj.addSheet(key) excelUtilObj.setKeys(worksheet) self.setExcel(worksheet, num, excelUtilObj, content, htmlLabel) num += 1 except urllib2.URLError, e: reason = e.reason pageCode = u"状态码:" + str(e.code) + u";错误原因:" + reason self.logUtil.setPostErrorLog(self.errorLogPath, url, pageCode) excelUtilObj.closeExcel()
class CookieUtil: def __init__(self): self.fileName = '../config/cookie.txt' self.iniObj = IniUtil('../config/config.ini') self.loginUrl = self.iniObj.getValue('loginUrl', 'url') #self.loginUrl = 'https://passport.suning.com/ids/login?method=GET&loginTheme=b2c' self.vCodeName = "../config/vcode.png" #MozillaCookieJar提供可读写操作的cookie文件,存储cookie对象 self.cookiejar = cookielib.MozillaCookieJar(self.fileName) #获取登录页面的response信息,用来获取cookie和uuid def getLoginPageResp(self): #将一个保存cookie对象,和一个HTTP的cookie的处理器绑定 cookieSupport = urllib2.HTTPCookieProcessor(self.cookiejar) #创建一个opener,将保存了cookie的http处理器,还有设置一个handler用于处理http的 opener = urllib2.build_opener(cookieSupport) #将包含了cookie、http处理器、http的handler的资源和urllib2对象绑定在一起,安装opener,此后调用urlopen()时都会使用安装过的opener对象, urllib2.install_opener(opener) #获取uuid sn = urllib2.urlopen(self.loginUrl) resp = sn.read() return resp #保存cookie到文件 #cookiejar.save(ignore_discard=True,ignore_expires=True) #获取uuid def getUuid(self, loginPageResp): #打开验证码 pattern = re.compile('var uuid = "(.*?)";', re.S) matchResult = re.search(pattern, loginPageResp) uuid = matchResult.group(1) #print "uuid::"+uuid return uuid #储存图片 def saveVCodeImg(self, uuid): imgurlStr = self.iniObj.getValue('codeImg', 'url') imgurl = imgurlStr + "?uuid=" + uuid + "&yys=" #imgurl = "https://vcs.suning.com/vcs/imageCode.htm?uuid="+uuid+"&yys=" request = urllib2.Request(imgurl) response = urllib2.urlopen(request) content = response.read() fp = open(self.vCodeName, "wb") fp.write(content) fp.close() #存储登录后cookie def saveLoginCookie(self, userName, passWord, uuid, vcode): data = urllib.urlencode({ 'username': userName, 'password': passWord, 'jsonViewType': 'true', 'loginTheme': 'b2c', "uuid": uuid, 'verifyCode': vcode }) request = urllib2.Request(self.loginUrl, data=data) response = urllib2.urlopen(request) #保存cookie到文件 self.cookiejar.save(ignore_discard=True, ignore_expires=True) #获取cookie def getCookie(self): #创建MozillaCookieJar实例对象 cookieFromFile = cookielib.MozillaCookieJar() #从文件中读取cookie内容到变量 cookieFromFile.load(self.fileName, ignore_expires=True, ignore_discard=True) return cookieFromFile #直接获取登录后cookie def getLoginCookie(self): resp_ = self.getLoginPageResp() uuid_ = self.getUuid(resp_) self.saveVCodeImg(uuid_) #输入验证码 vcode_ = raw_input("输入验证码 :") username_ = self.iniObj.getValue('user', 'username') password_ = self.iniObj.getValue('user', 'password') #username_ = '*****@*****.**' #password_ = 'yxz0521' self.saveLoginCookie(userName=username_, passWord=password_, uuid=uuid_, vcode=vcode_) returnCookie = self.getCookie() return returnCookie
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.errorLogPath = self.nowPath + "/../log/result_" + self.nowTile + "_error.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: self.logUtil.setKeyLog(self.errorLogPath, checkKeyResult) #校验成功 else: checkUrlResult = self.check.checkIniUrl() #url合法性校验失败 if checkUrlResult: self.logUtil.setUrlLog(self.errorLogPath, checkUrlResult) #校验成功 else: self.iniObj = IniUtil(self.nowPath + '/../config/url.ini') self.iniObjResult = IniUtil(self.resultIniPath) cookie = self.getCookie() #解析内容 self.getContent(cookie) #self.doPost(cookie) #self.setCode() #self.setHtml() print '执行完毕!!' def getCookie(self): cookie = self.cookieObj.getLoginCookie() return cookie def getContent(self, cookie): #获取配置文件的url keysList = [] urlDict = {} keysList = self.iniObj.getKeysBySection('url') num = 1 excelUtilObj = ExcelUtil() for key in keysList: url = self.iniObj.getValue('url', key) urlDict.setdefault(key, url) try: #创建请求的request req = urllib2.Request(url) handler = urllib2.HTTPCookieProcessor(cookie) #利用urllib2的build_opener方法创建一个opener openner = urllib2.build_opener(handler) response = openner.open(req) content = response.read() htmlLabel = excelUtilObj.htmlLabel worksheet = excelUtilObj.addSheet(key) excelUtilObj.setKeys(worksheet) self.setExcel(worksheet, num, excelUtilObj, content, htmlLabel) num += 1 except urllib2.URLError, e: reason = e.reason pageCode = u'状态码:' + str(e.code) + u';错误原因:' + reason self.logUtil.setPostErrorLog(self.errorLogPath, url, pageCode) excelUtilObj.closeExcel()
class CookieUtil: def __init__(self): self.fileName = '../config/cookie.txt' self.iniObj = IniUtil('../config/config.ini') self.loginUrl = self.iniObj.getValue('loginUrl','url') #self.loginUrl = 'https://passport.suning.com/ids/login?method=GET&loginTheme=b2c' self.vCodeName = "../config/vcode.png" #MozillaCookieJar提供可读写操作的cookie文件,存储cookie对象 self.cookiejar = cookielib.MozillaCookieJar(self.fileName) #获取登录页面的response信息,用来获取cookie和uuid def getLoginPageResp(self): #将一个保存cookie对象,和一个HTTP的cookie的处理器绑定 cookieSupport= urllib2.HTTPCookieProcessor(self.cookiejar) #创建一个opener,将保存了cookie的http处理器,还有设置一个handler用于处理http的 opener = urllib2.build_opener(cookieSupport) #将包含了cookie、http处理器、http的handler的资源和urllib2对象绑定在一起,安装opener,此后调用urlopen()时都会使用安装过的opener对象, urllib2.install_opener(opener) #获取uuid sn = urllib2.urlopen(self.loginUrl) resp = sn.read() return resp #保存cookie到文件 #cookiejar.save(ignore_discard=True,ignore_expires=True) #获取uuid def getUuid(self,loginPageResp): #打开验证码 pattern = re.compile('var uuid = "(.*?)";',re.S) matchResult = re.search(pattern,loginPageResp) uuid = matchResult.group(1) #print "uuid::"+uuid return uuid #储存图片 def saveVCodeImg(self,uuid): imgurlStr = self.iniObj.getValue('codeImg','url') imgurl = imgurlStr + "?uuid="+uuid+"&yys=" #imgurl = "https://vcs.suning.com/vcs/imageCode.htm?uuid="+uuid+"&yys=" request = urllib2.Request(imgurl) response = urllib2.urlopen(request) content = response.read() fp = open(self.vCodeName,"wb") fp.write(content) fp.close() #存储登录后cookie def saveLoginCookie(self,userName,passWord,uuid,vcode): data = urllib.urlencode({ 'username':userName, 'password':passWord, 'jsonViewType':'true', 'loginTheme':'b2c', "uuid":uuid, 'verifyCode':vcode }) request = urllib2.Request(self.loginUrl,data=data) response = urllib2.urlopen(request) #保存cookie到文件 self.cookiejar.save(ignore_discard=True,ignore_expires=True) #获取cookie def getCookie(self): #创建MozillaCookieJar实例对象 cookieFromFile = cookielib.MozillaCookieJar() #从文件中读取cookie内容到变量 cookieFromFile.load(self.fileName,ignore_expires=True,ignore_discard=True) return cookieFromFile #直接获取登录后cookie def getLoginCookie(self): resp_ = self.getLoginPageResp() uuid_ = self.getUuid(resp_) self.saveVCodeImg(uuid_) #输入验证码 vcode_ = raw_input("输入验证码 :") username_ = self.iniObj.getValue('user','username') password_ = self.iniObj.getValue('user','password') #username_ = '*****@*****.**' #password_ = 'yxz0521' self.saveLoginCookie(userName=username_,passWord=password_,uuid=uuid_,vcode=vcode_) returnCookie = self.getCookie() return returnCookie
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)