interfaceDriver.py 该脚本提供的接口是为代理获取的数据包做准备,与常规的get与post方法不一样!之前的博客有些GET与POST的请求python脚本: #coding=utf8 ''' 作者:ewang 版本号:V1.0 日期:201
interfaceDriver.py
该脚本提供的接口是为代理获取的数据包做准备,与常规的get与post方法不一样!之前的博客有些GET与POST的请求python脚本:
#coding=utf8'''
作者:ewang
版本号:V1.0
日期:2017/11/06
描述:
该模块是用来与代理结合,来进行接口测试的代码模块。
使用GET与POST方法进行http请求,该模块主要包包含如下方法:
Method(method):执行post与get方法
Status():响应的状态码
GetResponeInfo():获取响应返回的文本信息
'''
import urllib
import urllib2
class 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='http://blog.***.net/henni_719'
req=requestMethod('GET',url)
print req.Status()
print req.GetResponeInfo()
if __name__=="__main__":
test()
执行结果截图:
如果对测试比较感兴趣的可以加QQ群:320542475!