1.环境搭建
1.1.安装visual studio code(vscode) 、python3
1.2.安装requests库
在终端输入pip3 install requests (适用于python3)
2.发送Post请求
导入 requests、json库
import requests
import json
url = 'https://127.0.0.1/dc-wisdom-manager/ipad/receptionDeskAuthor/login'
data = {
"loginName":"18888002222",
"password":"chen@123",
"identityType":1
}
# verify为https验证
res_post = requests.post(url,data,verify=False).json()
# 输出json格式
print(json.dumps(res_post,indent=4,ensure_ascii=False))
3.发送get请求
get_url = 'https://test-client.polyxfb.com/dc-wisdom-manager/app/api/sign/in?loginName=18766662233&password=Poly@123&identityType=1' #案场登录
res_get =requests.get(get_url,verify=False).json() #告诉程序为一个json格式
print(json.dumps(res_get,indent=4,ensure_ascii=False)) #使用josn库中dumps方法格式化
exit()
4.上传文件
上传图片
import json
import requests
from requests_toolbelt import MultipartEncoder # 上传图片需要引入MultipartEncoder方法
import unittest
def file_upload(self):
header = self.request_login()
upload_url = "http://192.168.0.200/api/file/file/upload"
upload_file = {
'typeValue':'1',
'file':('xmind.png',open('C:/Users/zeewain/Downloads/xmind.png','rb'),"image/png")
}
m = MultipartEncoder(upload_file)
header['Content-Type']=m.content_type
upload_res = requests.post(upload_url,data=m,headers=header).json()
upload_res = json.dumps(upload_res,indent=4,ensure_ascii=False)
return upload_res
5.下载文件
dl_url = 'http://file.mukewang.com/apk/app/139/1642579764/imooc_8.1.6_10102001_android.apk?version=1642579767'
dl_res = requests.get(dl_url)
with open('imooc_8.1.6_10102001_android.apk','wb') as f:
f.write(dl_res.content)
print(dl_res)
exit()
6.请求头加密串处理
引入库 import hashlib->
import hashlib
import requests
import json
imooc = 'imooc.com'
md5 = hashlib.md5() # md5实例化对象
md5.update(imooc.encode('utf-8')) # MD5加密并转码
res = md5.hexdigest() #获取加密结果
print(res)
转换字符串加密
data = str({
'user':'Cheney'
})
md5 = hashlib.md5()
md5.update(data.encode('utf-8'))
res1 = md5.hexdigest()
print(res1)
在请求头使用加密的变量
header = {
'Host':'m.imooc.com',
'Connection':'keep-alive',
'Pragma':'no-cache',
'Cache-Control':'no-cache',
'Accept':'application/json, text/javascript, */*; q=0.01',
'X-Requested-With':'XMLHttpRequest',
'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
'Referer':'https://m.imooc.com/',
'Accept-Language':'zh-CN,zh;q=0.9',
'token':res,
'psid':res1
}
res = requests.get('https://m.imooc.com/api/search/searchword',headers=header,verify=False).json()
print(res)