方法1:
1.安装requests_toolbelt依赖库
#代码实现 def upload(self): login_token = self.token.loadTokenList() for token in login_token: tempPassword_url = self.config['crm_test_api']+'/document/upload' tempPassword_data = self.data_to_str.strToDict('''title:1.png course_name_id:63 course_id:1112 desc:7 doc_type:1 is_public:1''',value_type='str') files={'file': ('1.png', open('C:\\Users\\Acer\\Pictures\\Screenshots\\1.png', 'rb'), 'image/png')} tempPassword_data.update(files) m = MultipartEncoder( fields=tempPassword_data ) tempPassword_headers = {"Content-Type": m.content_type, "token": token} tempPassword_request = requests.post(url=tempPassword_url,data=m,headers=tempPassword_headers) print(tempPassword_request.content)
2.组装MultipartEncoder对象需要的参数:将tempPassword_data的字段合并至files
1.files参数介绍:
1.字典key对应file字段(我们系统是这样,具体结合前端实际的字段为准),如图
2.字典value里面的对象:
1.filename(服务器最终存储的文件名)
2.filepath(具体的文件路径,注意转义),文件是以二进制的形式进行传输的,所以这里传输时以二进制的形式打开文件并传输
3.content_type:具体结合前端实际的字段为准:一般可定义为: 文本(text)/图片(image)等[/code][code]
3.tempPassword_data:为文件上传时的附带参数
strToDict方法:自己手写的一个字符串转dict的方法
遇到的问题:
这个错误是说,int对象不能被编码,所以需要手动将int对象转换为str,所以我在此方法中定义了value_type这个参数,用于将字典中的所有value转换为str类型
#具体代码实现,仅供参考 def strToDict(str_in,value_type=None): # value_type:转换字典的value为指定的类型,未防止异常,目前仅支持str # '''将str转换为dict输出''' # '''将带有time关键字的参数放到字符串末尾''' # print(str_in) if str_in: match_str = ':' split_str = '\n' split_list = str_in.split(split_str) str_in_dict = {} for i in split_list: colon_str_index = i.find(match_str) if colon_str_index == -1: # '''处理firefox复制出来的参数''' match_str = '\t' or ' ' colon_str_index = i.find(match_str) # '''去掉key、value的空格,key中的引号''' str_in_key = i[:colon_str_index].strip() str_in_key = str_in_key.replace('"','') str_in_key = str_in_key.replace("'",'') # 正则过滤无用key,只保留key第一位为字母数据获取[]_ str_sign = re.search('[^a-zA-Z0-9\_\[\]+]', str_in_key[0]) if str_sign is None: # 处理value中的空格与转义符 str_in_value = i[colon_str_index + 1:].strip() str_in_value=str_in_value.replace('\\','') try: # 遇到是object类型的数据转换一下 str_in_value=eval(str_in_value) except BaseException as error: str_in_value=str_in_value if value_type in ['str','string']: str_in_value=str(str_in_value) else: str_in_value=str_in_value str_in_dict[str_in_key] = str_in_value return str_in_dict else: print("参数都没有,还处理个球嘛") return None
3.请求时将headers的content设置为m.content_type,会设置headers的content_type为form—data,类型为str:
MultipartEncoder相关源码:
4.请求时设置data为m,会输出一个MultipartEncoder对象:
方法2:
直接使用requests,无需依赖requests_toolbelt库
过程大同小异,也是需要将字典的value转换为str
注意:headers不要传content_type字段,headers不要传content_type字段,headers不要传content_type字段
请求时:data对应附加参数,files对应files对象
#相关代码 def upload(self): login_token = self.token.loadTokenList() for token in login_token: tempPassword_url = self.config['crm_test_api']+'/document/upload' tempPassword_data = self.data_to_str.strToDict('''title:1.png course_name_id:63 course_id:1112 desc:7 doc_type:1 is_public:1''',value_type='str') files={'file': ('1.png', open('C:\\Users\\Acer\\Pictures\\Screenshots\\1.png', 'rb'), 'image/png')} tempPassword_headers = {"token": token} tempPassword_request = requests.post(url=tempPassword_url,data=tempPassword_data,files=files,headers=tempPassword_headers) print(tempPassword_request.json())
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]