美文网首页
【python】requests 调用上传照片接口,接口有多个f

【python】requests 调用上传照片接口,接口有多个f

作者: GovS_777 | 来源:发表于2018-09-19 01:04 被阅读0次
        # 上传照片处理, 若需要上传多个附件,返回如下dict
        def upload_files_format(self, filedata, path):
            """
            :param filedata:        # 对于key必传,value非传的附件, 则传"" 即可。  eg: filedata = {"filename1": ""}
            :param path: 
            :return:         
                {
                  "field1" : open("filePath1", "rb")),
                  "field2" : open("filePath2", "rb")),
                  "field3" : open("filePath3", "rb"))
                }
            """
            upload_files = {}
            for key in filedata.keys():
                if filedata[key] != "":
                    filedata[key] = path + filedata[key]
                else:
                    filedata[key] = ""
            for (k, v) in filedata.items():
                if v != "":
                    vfile = open(v, 'rb')
                    upload_files[k] = vfile
                else:
                    upload_files[k] = ""
            return upload_files
    
    # 调用, 其中filedata为图片相对路径地址, path为公共路径地址;  eg:path = os.getcwd() ;  filedata = /pic/1/jpg
    upload_files = self.upload_files_format(local_files, path)
    response = self.session.post(url, data=json, files=upload_files, headers=headers, verify=False)
    

    相关文章

      网友评论

          本文标题:【python】requests 调用上传照片接口,接口有多个f

          本文链接:https://www.haomeiwen.com/subject/behbnftx.html