zoukankan      html  css  js  c++  java
  • python-上传文件的几种方式

    from requests_toolbelt import MultipartEncoder
    import requests
    
    # from_data上传文件,注意参数名propertyMessageXml
    data = MultipartEncoder(fields={'propertyMessageXml': ('filename', open('D:/123.xml', 'rb'), 'text/xml')})
    requests.post(url=url,data=data,headers={ 'Content-Type': data.content_type})
    
    #raw上传文件
    file = open('D:/123.xml','rb')
    requests.post(url=url,data=file.read(),headers={'Content-Type':'text/xml'})
    
    #binary上传文件
    files={'file':open('D:/123.xml','rb')}
    requests.post(url=url,files=files,headers={'Content-Type':'binary'})
    import requests,glob
    from urllib3 import encode_multipart_formdata
    
    def upload_file(url=None,path=None,file_path=None):
        if path:
            for file_path in glob.glob(path + '*'): #批量文件
                data={}
                data['file'] = (file_path.split("/")[-1], open(file_path, 'rb').read())  # 名称,读文件
                encode_data = encode_multipart_formdata(data)
                res = requests.post(url, headers={'Content-Type':encode_data[1]},data=encode_data[0])
                return res.text
        if file_path:
            data = {}
            data['file'] = (file_path.split("/")[-1], open(file_path, 'rb').read())  # 名称,读文件
            encode_data = encode_multipart_formdata(data)
            res = requests.post(url, headers={'Content-Type': encode_data[1]}, data=encode_data[0])
            return res.text
  • 相关阅读:
    vim 命令
    navicat 破解
    vim 使用技巧记录
    Ubuntu 16.04 安装ftp服务器
    mockito测试入门学习
    Java的getClass()函数
    JQuery中Ajax的操作
    JS中获取元素使用getElementByID()、getElementsByName()、getElementsByTagName()的用法和区别
    Tomcat下ajax请求路径总结
    javascript:void()的理解
  • 原文地址:https://www.cnblogs.com/shuzf/p/11972116.html
Copyright © 2011-2022 走看看