我们在使用python进行接口自动化测试过程中,可能会用到httplib或者requests或者urllib2库,那么怎么模拟http的请求呢,今天小编大概写几个例子吧。。。
def httpget(url):
try:
conn = httplib.HTTPConnection(url)
conn.request('GET',url)
response = conn.getresponse().read()
realRes = unicode(response,'utf-8')
except Exception, e:
print e
return realRes
def httppost(url,params):
res = requests.post(url, data=params)
realRes = res.text
return realRes
#上传文件接口
def httppostUploadfile(url,parmas):
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
MultipartPostHandler.MultipartPostHandler)
res = opener.open(url, parmas)
return opener.open(url, parmas).read()
大家如果有感兴趣的方向文章,可以直接给我留言,我会去写你们感兴趣的文章。
想要了解,获取更多和测试相关的知识请添加此群,本群提供App、Web等Ui自动化,App性能测试,接口功能,
接口自动化测试,jmeter、loadrunner工具介绍,静态代码扫描实践;测试理论,
测试流程。测试管理。
App自动化及Web自动化全栈测试:群号544126564(想要更多资料立即加入此群)
领取专属 10元无门槛券
私享最新 技术干货