Python接口测试之cookies传值
在做接口自动化时,大家应该都会遇到后续接口需要cookies,否则请求报错,废话不多说,直接贴码
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
#获取cookies对象
cookiejar=cookielib.CookieJar();
#自定义urlOpener,并将其与Cookies对象绑定
urlOpener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
headers={"User-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1"};
data={'username':'wu.xiaohong','password':'123456'};
postdata = urllib.urlencode(data);
url_1='http://IP:端口/login';
request=urllib2.Request(url_1,postdata,headers);
url=urlOpener.open(request);
url_2='http://IP:端口/api/repayment/queryAmount?bid=1351793&period=3&prapay=false'
request=urllib2.Request(url_2)
url= urlOpener.open(request)
page=url.read()
print page