zoukankan      html  css  js  c++  java
  • python上传文件接口

    文件的参数组装:

        ('文件名',"open打开的文件(rb模式打开)",'文件的类型说明')

     

    关于不同的请求参数类型,使用requests的处理:

    1、文件上传(Content-Type: multipart/form-data;),使用files传递

        requests.post(url='xxx',files=文件参数)

        

    2、表单参数(Content-Type: application/x-www-form-urlencoded;),使用data传递

        requests.post(url='xxx',data=表单参数)

     

    3json参数(Content-Type: application/json),使用json传递

        requests.post(url='xxx',json=json参数)

     

    4、查询字符串(拼接在url地址后面的),params传递

        requests.post(url='xxx',params=查询字符串参数)

     

    """

    import requests
    
     
    
    # 第一步:准备请求接口所需的数据
    
    # 1、接口地址
    
    url = "http://127.0.0.1:5000/upload"
    
    
    # 2、请求参数
    
    params = {
    
        "nickname": "木森",
    
        "age": 18,
    
        "sex": ""
    
    }
    
    # 文件的参数组装
    
    file = {
    
        "pic": ("kejian.ppt", open(r"C:课件课件模板.ppt", "rb"), "text/txt")
    
    }
    
     
    
    # 使用多个参数上传多个文件的时候,参数的组装形式
    
    # file = {
    
    #     "pic": ("kejian.ppt", open(r"C:课件课件模板.ppt", "rb"), "text/txt"),
    
    #     "pic2": ("lmb.png", open("lmb.png", "rb"), "text/txt")
    
    # }
    
     
    
    # 同一个参数上传多个文件的时候,参数的组装形式
    
    # files = [
    
    #     ("pic", ("kejian.ppt", open(r"C:课件课件模板.ppt", "rb"), "text/txt")),
    
    #     ("pic", ("lmb.png", open("lmb.png", "rb"), "text/txt")),
    
    # ]
    
    # 第二步发送请求 params  data  json,files
    
    # 文件上传的接口,上传的文件需要使用files来进行传递
    
    res = requests.post(url=url, data=params, files=file)
    
    # 第三步:获取返回结果
    
    print(res.json())
  • 相关阅读:
    flume+kafka+storm打通过程
    kafka入门
    hive执行更新和删除操作
    redis存储对象与对象序列化详解
    语音常识--语音信号的数字模型
    声源测向: TDOA-GCC-PATH方法
    【面试】如何比较一个类型【模板使用】【sizeof用法】
    【概括】C++11/14特性
    【面试】编译器为我们实现了几个类成员函数?(c++)
    语音信号处理常识【摘抄|自用】
  • 原文地址:https://www.cnblogs.com/crystal1126/p/13591049.html
Copyright © 2011-2022 走看看