python3为什么不提供接口_python3-requests发送不同类型的接口请求

发送get请求

import requests

# request发送http请求

def test_no_params():

# 使用requests.get发送一个get请求。

# r = requests.get("https://www.baidu.com/")

# 使用requests.request发送一个get请求。

# r = requests.request(method="GET",url="https://www.baidu.com/")

sess = requests.session() # 使用session建立连接

r = sess.request(method="GET",url="https://www.baidu.com/") # 再发送请求,无论请求多少次,连接只建立

r = sess.request(method="GET", url="https://www.baidu.com/")

print(r.text)#text获取响应对象中,响应正文的数据

def test_get_query():

# get请求带参数

pa = {"accountName":"xuepl123"} # 把get参数都放入一个字典中

r = requests.request("GET","http://qa.yansl.com:8084/acc/getAccInfo",params=pa) # 请求时以params关键字传参

print(r.text)

def test_get_path():

# get请求参数在path中

# 使用.format进行字符串格式化

r = requests.request("GET","http://qa.yansl.com:8084/acc/getAllAccs/{pageNum}/{pageSize}".format(pageNum=1,pageSize=10))

print(r.text)

def test_get_file(pub_data):

# get请求下载文件

p = {"pridCode":"63803y"}

h={"token":pub_data["token"]}

r = requests.request("GET","http://qa.yansl.com:8084/product/downProdRepertory",params=p,headers=h)

# r.content获取响应正文的字节码(二进制)

with open("aa.xls","wb") as f: # with语法 打开文件,并赋值给变量f

f.write(r.content) # 把响应中的数据写入到文件中

发送post请求

import requests

def test_post_json_1(pub_data):

data = {

"pwd": "abc123",

"userName": "tuu653"

}

h = {"token": pub_data["token"]}

r = requests.post("http://qa.yansl.com:8084/login",json=data,headers=h) # json关键字发送json类型数据

print(r.text)

def test_post_json_2(pub_data):

data = '''{

"pwd": "abc123",

"userName": "tuu653"

}'''

h = {"token": pub_data["token"],"content-type":"application/json"}

r = requests.post("http://qa.yansl.com:8084/login",data=data,headers=h) # data关键字发送json类型数据,请求头中必须指定content-type

print(r.text)

def test_post_formdata(pub_data):

# post请求键值对数据

data = {

"userName": "tan242743"

}

h = {"token": pub_data["token"]}

r = requests.post("http://qa.yansl.com:8084/user/lock",data=data,headers=h) # data关键字发送键值对类型数据

print(r.text)

def test_post_upload_file(pub_data):

# post请求上传文件

data = {

"file": open("aa.xls","rb")

}

h = {"token": pub_data["token"]}

r = requests.post("http://qa.yansl.com:8084/product/uploaProdRepertory", files=data, headers=h) # files关键字发送文件类型数据

print(r.text)

上传文件接口

def test_post_upload_file(pub_data):

# post请求上传文件

data = {

"file": open("aa.xls","rb")

}

h = {"token": pub_data["token"]}

r = requests.post("http://qa.yansl.com:8084/product/uploaProdRepertory", files=data, headers=h) # files关键字发送文件类型数据

print(r.text)

下载文件接口

def test_get_file(pub_data):

# get请求下载文件

p = {"pridCode":"63803y"}

h={"token":pub_data["token"]}

r = requests.request("GET","http://qa.yansl.com:8084/product/downProdRepertory",params=p,headers=h)

# r.content获取响应正文的字节码(二进制)

with open("aa.xls","wb") as f: # with语法 打开文件,并赋值给变量f

f.write(r.content) # 把响应中的数据写入到文件中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值