以下为python3接口测试范本
# -*- coding:utf-8 -*-import requests #先导入包,这是必须的url = 'http://XXXXXXXXXXXXXXX' #url:接口地址data = { 'XXXX':'XXX'} #data:接口传递的参数headers = { 'Connection':'close'} #header:传递header信息 #files:接口中需要上传文件则需要用到该参数r = requests.post(url,data = data,headers = headers) #请求url,获得返回的数据信息print(r.text.encode('utf-8')) #获得的返回数据使用text方法进行获取
或者另一种方式也一样,代码如下:
# -*- coding:utf-8 -*-import urllib.requestimport requests #先导入包,这是必须的import urllib.parseurl = 'XXXXXXX' # url:接口地址data = { 'XXX': 'XXX'} #data:接口传递的参数headers = { 'Connection': 'close'} # header:传递header信息def requests_test(url,data,headers): response = requests.post(url,data = data,headers = headers) #请求url,获得返回的数据信息 print(response.text,response.headers) #返回头部信息def urllib_test(url,data): data1 = urllib.parse.urlencode(data).encode('utf-8') response = urllib.request.Request(url=url,data = data1) html = urllib.request.urlopen(response) print(html.read()) print(html.getcode(),html.msg) #获得html返回的状态 print(html.headers) #返回头部信息#下面调用两个方法:urllib_test(url,data)requests_test(url,data,headers)
关于这两个库的get请求,我就不多讲了。
requests: requests.get("url")
urllib:不传入data就可以了。如下:
get 方法
import urllib.requestimport urllib.parseurl='http://www.baidu.com're=urllib.request.Request(url)html=urllib.request.urlopen(re)print(html.getcode(),html.msg)# print(html.read())
如果要解决接口返回值的unicode编码,则
html.read().decode('unicode-escape')