import requests
#get请求
url = "http://127.0.0.1:8000/login"
data={"username":"jmy1235","pwd":123456}
req = requests.get(url+"?"+"username=jmy1235&pwd=123456")
req = requests.get(url,data)
print(req.text) #字符串
##post请求 url = "http://127.0.0.1:8000/login" data={"username":"jmy1235","pwd":123456} req = requests.post(url+"?"+"username=jmy1235&pwd=123456") req = requests.post(url,data) print(req.text) #字符串 print(req.json()) #返回的是字典,如果接口返回的是json的话,那么才能用.json
#请求报文为json类型 def get_sign(url,data): res = requests.post(url,data).json() print(res) return res['sign'] print(get_sign(url,data)) sign = get_sign(url,data) data2 = { "username":'jmy1235', "real_name":"处长", "class":"巨蟹座", "phone":"19312345673", } r = requests.post('http://127.0.0.1:8000/add_stu',json=data2) #请求报文为json类型的 print(r.text) ##发带cookie和header sign = get_sign(url,data) cookie = {'sign':sign} header = {'tangxiaofeng':'sevenqq'} data2 = { "username":'jmy1235', "real_name":"孙树江", "class":"巨蟹座", "phone":"19312345674" } url = "http://127.0.0.1:8000/add_stu2" r = requests.post(url,json=data2,cookies=cookie,headers=header) #header里面传的时候不能有中文 print(r.text) #下面是上传文件的 file ={ 'file_name':open('hahah.py',encoding='utf-8') } data = {"username":"jmy",'pwd':123456} url="http://127.0.0.1:8000/upload" r = requests.post(url,data,files=file) print(r.text)
重新起航!