• (一)python发送HTTP 请求的两种方式(get和post )


    引入 requests 包

    import requests

    注:发送请求(包括请求行、方法类型、头、体) & 常见的请求方式有get、post、put、delete

    一、发送get请求

               格式:requests.get() (内容: url必填; params选填:url参数字典)

    # ~ 无参数的get请求
    1. res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince')
    2. print(res.text)
    3. #打印响应主体内容,字符串格式
    # ~ 有参数的get请求
    1. res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString?theRegionCode=3113')
    2. print(res.text)
    # ~ 使用params的get请求
    1. res = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',
    2. params={"theRegionCode":3113})
    3. print(res.text)

    二、发送post请求


           知识扩展#  requests.post() post请求分为5种,常用的有三种,如下
          ①application/x-www-form-urlencod (form表单);
          ②raw (纯文本格式):有5种格式,分别为json/xml/Html/Text/JavaScrip
          ③multipart/form-data (复合式表单)

    # ~ 无参数的post请求
    # ~ 有正文体的post请求


    1.   form表单(application/x-www-form-urlencod)        

    -----data   后跟字典

    eg.1: 带参数的查询接口

    1. res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getSupportCityString',
    2. headers={"Content-Type": "application/x-www-form-urlencoded"},
    3. data={"theRegionCode": 3113})
    4. print(res.text)


      eg.2: 带账号名密码的登录接口

    1. res = requests.post(url='http://123.56.99.53:9000/event/api/admin/',
    2. headers={"Content-Type": "application/x-www-form-urlencoded"},
    3. data={"username": "admin", "password": "MTIzYWRtaW4="})
    4. print(res.text)

    2.   纯文本格式(raw)

     # ~ xml格式    ----data    后跟字符串

    1. res = requests.post(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx',
    2. headers ={"Content-Type": "text/xml; charset=utf-8",
    3. "SOAPAction": "http://WebXml.com.cn/getSupportCityString"},
    4. data = '''
    5. string
    6. ''')
    7. print(res.text)

      # ~json格式       ----- json  后跟字典(常用), or ----data  后跟json字符串

    1. res = requests.post(url='http://123.56.99.53:5000/event/weather/getWeather/',
    2. headers={"Content-Type": "application/json"},
    3. json={"theCityCode": "1001"})
    4. print(res.text)

    注:# json后面跟的是字典即json={"theCityCode": "1001"}),可改成 data=‘{"theCityCode": "1001"})’;字典能自动转为json字符串,所以一般用字典表示;建议纯json正文的接口使用第一种方式,除非是较长的字符串     

    3.  复合式表单(multipart/form-data )  

      ------上传二进制文件    ---- key value (接口名称:文件名)

    1. res = requests.post(url='http://123.56.99.53:9001/api/uploadFile/',
    2. headers={"Cookie": "uid=1;token=44c972f05d76fdd93c31f9c2b65bb098f308cdfc"
    3. #"Content-Type": "multipart/form-data"},
    4. #有的接口不需要写正文体格式,老服务器一般需要写
    5. files={"myfile1": open('D:\全力以富\1.docx', 'rb')
    6. #"myfile2: open(上传多个文件)"})
    7. print(res.text)

    注:上传接口 files={‘文件路径’,‘rb'}  rb表示

  • 相关阅读:
    【华为OD机试真题 python】仿lisp运算【2022 Q4 | 200分】
    【AI视野·今日CV 计算机视觉论文速览 第255期】Wed, 27 Sep 2023
    对pure pursuit算法的理解和改进
    【数据库】形式化关系查询语言(一):关系代数Relational Algebra
    Java8中有哪些原子类呢?
    【问题思考总结】块和页框的大小相等吗?(从抽象层次的角度思考)
    基于Vite初始化前端项目
    Git 之 push 代码后,如何回退/回滚到之前的版本的方法
    nginx配置vue前端代理
    java计算机毕业设计基于安卓Android微信小程序的应急求救信息发布系统小程序uniAPP
  • 原文地址:https://blog.csdn.net/fanfangyu/article/details/134245609