当前位置:   article > 正文

一.Request方法运用_visual studio怎么安装requests

visual studio怎么安装requests

1.环境搭建

1.1.安装visual studio code(vscode) 、python3

1.2.安装requests库

在终端输入pip3 install requests (适用于python3)

2.发送Post请求

导入 requests、json库

  1. import requests
  2. import json
  3. url = 'https://127.0.0.1/dc-wisdom-manager/ipad/receptionDeskAuthor/login'
  4. data = {
  5. "loginName":"18888002222",
  6. "password":"chen@123",
  7. "identityType":1
  8. }
  9. # verify为https验证
  10. res_post = requests.post(url,data,verify=False).json()
  11. # 输出json格式
  12. print(json.dumps(res_post,indent=4,ensure_ascii=False))

3.发送get请求

  1. get_url = 'https://test-client.polyxfb.com/dc-wisdom-manager/app/api/sign/in?loginName=18766662233&password=Poly@123&identityType=1' #案场登录
  2. res_get =requests.get(get_url,verify=False).json() #告诉程序为一个json格式
  3. print(json.dumps(res_get,indent=4,ensure_ascii=False)) #使用josn库中dumps方法格式化
  4. exit()

4.上传文件

上传图片

  1. import json
  2. import requests
  3. from requests_toolbelt import MultipartEncoder # 上传图片需要引入MultipartEncoder方法
  4. import unittest
  5. def file_upload(self):
  6. header = self.request_login()
  7. upload_url = "http://192.168.0.200/api/file/file/upload"
  8. upload_file = {
  9. 'typeValue':'1',
  10. 'file':('xmind.png',open('C:/Users/zeewain/Downloads/xmind.png','rb'),"image/png")
  11. }
  12. m = MultipartEncoder(upload_file)
  13. header['Content-Type']=m.content_type
  14. upload_res = requests.post(upload_url,data=m,headers=header).json()
  15. upload_res = json.dumps(upload_res,indent=4,ensure_ascii=False)
  16. return upload_res

5.下载文件

  1. dl_url = 'http://file.mukewang.com/apk/app/139/1642579764/imooc_8.1.6_10102001_android.apk?version=1642579767'
  2. dl_res = requests.get(dl_url)
  3. with open('imooc_8.1.6_10102001_android.apk','wb') as f:
  4. f.write(dl_res.content)
  5. print(dl_res)
  6. exit()

6.请求头加密串处理

引入库 import hashlib->

  1. import hashlib
  2. import requests
  3. import json
  4. imooc = 'imooc.com'
  5. md5 = hashlib.md5() # md5实例化对象
  6. md5.update(imooc.encode('utf-8')) # MD5加密并转码
  7. res = md5.hexdigest() #获取加密结果
  8. print(res)

转换字符串加密

  1. data = str({
  2. 'user':'Cheney'
  3. })
  4. md5 = hashlib.md5()
  5. md5.update(data.encode('utf-8'))
  6. res1 = md5.hexdigest()
  7. print(res1)

在请求头使用加密的变量

  1. header = {
  2. 'Host':'m.imooc.com',
  3. 'Connection':'keep-alive',
  4. 'Pragma':'no-cache',
  5. 'Cache-Control':'no-cache',
  6. 'Accept':'application/json, text/javascript, */*; q=0.01',
  7. 'X-Requested-With':'XMLHttpRequest',
  8. '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',
  9. 'Referer':'https://m.imooc.com/',
  10. 'Accept-Language':'zh-CN,zh;q=0.9',
  11. 'token':res,
  12. 'psid':res1
  13. }
  14. res = requests.get('https://m.imooc.com/api/search/searchword',headers=header,verify=False).json()
  15. print(res)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/292355?site
推荐阅读
相关标签
  

闽ICP备14008679号