29、OSPF配置实验之被动接口
331
2022-08-28
使用urllib与urllib包实现GET与POST方法请求(interfaceDriver.py)
interfaceDriver.py
该脚本提供的接口是为代理获取的数据包做准备,与常规的get与post方法不一样!之前的博客有些GET与POST的请求python脚本:
#coding=utf8'''作者:ewang版本号:V1.0日期:2017/11/06描述:该模块是用来与代理结合,来进行接口测试的代码模块。使用GET与POST方法进行urllibimport urllib2class requestMethod(object): def __init__(self,method,url,headers=None,data=None): self.method=method self.url=url if headers: self.headers=headers else: self.headers=None if data: self.data=urllib.urlencode(data) else: self.data=None def Method(self,method): try: if method =='POST': if self.url and self.headers and self.data: req=urllib2.Request(url=self.url,headers=self.headers,data=self.data) try: ResponeData=urllib2.urlopen(req) except urllib2.HTTPError,e: print "Error code:",e.code,":",e._reason except urllib2.URLError,e: print "Unreach to server:",e._reason else: return ResponeData else: print "POST Request Error!" elif method=='GET': if self.url: req=urllib2.Request(self.url,self.headers) try: ResponeData=urllib2.urlopen(req) except urllib2.HTTPError,e: print "Error code:",e.code,":",e._reason except urllib2.URLError,e: print "Unreach to server:",e._reason else: return ResponeData else: print "GET Request Error!" else: print "URL IS NULL!" except Exception,e: print "Method Error:",e def Status(self): try: if self.method: ResponeData=self.Method(self.method) return ResponeData.getcode() else: print "Method is Null!" except Exception,e: print e def GetResponeInfo(self): try: if self.method: ResponeData=self.Method(self.method) return ResponeData.read() else: print "Method is Null!" except Exception,e: print e def test(): url=' req=requestMethod('GET',url) print req.Status() print req.GetResponeInfo() print if __name__=="__main__": test()
执行结果截图:
如果对测试比较感兴趣的可以加QQ群:320542475!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~