Python基于requests實(shí)現(xiàn)模擬上傳文件
方法1:
1.安裝requests_toolbelt依賴庫(kù)
#代碼實(shí)現(xiàn)
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對(duì)象需要的參數(shù):將tempPassword_data的字段合并至files
1.files參數(shù)介紹:
1.字典key對(duì)應(yīng)file字段(我們系統(tǒng)是這樣,具體結(jié)合前端實(shí)際的字段為準(zhǔn)),如圖

2.字典value里面的對(duì)象:
1.filename(服務(wù)器最終存儲(chǔ)的文件名)
2.filepath(具體的文件路徑,注意轉(zhuǎn)義),文件是以二進(jìn)制的形式進(jìn)行傳輸?shù)?,所以這里傳輸時(shí)以二進(jìn)制的形式打開(kāi)文件并傳輸
3.content_type:具體結(jié)合前端實(shí)際的字段為準(zhǔn):一般可定義為: 文本(text)/圖片(image)等[/code][code]
3.tempPassword_data:為文件上傳時(shí)的附帶參數(shù)
strToDict方法:自己手寫(xiě)的一個(gè)字符串轉(zhuǎn)dict的方法
遇到的問(wèn)題:

這個(gè)錯(cuò)誤是說(shuō),int對(duì)象不能被編碼,所以需要手動(dòng)將int對(duì)象轉(zhuǎn)換為str,所以我在此方法中定義了value_type這個(gè)參數(shù),用于將字典中的所有value轉(zhuǎn)換為str類型
#具體代碼實(shí)現(xiàn),僅供參考
def strToDict(str_in,value_type=None):
# value_type:轉(zhuǎn)換字典的value為指定的類型,未防止異常,目前僅支持str
# '''將str轉(zhuǎn)換為dict輸出'''
# '''將帶有time關(guān)鍵字的參數(shù)放到字符串末尾'''
# 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復(fù)制出來(lái)的參數(shù)'''
match_str = '\t' or ' '
colon_str_index = i.find(match_str)
# '''去掉key、value的空格,key中的引號(hào)'''
str_in_key = i[:colon_str_index].strip()
str_in_key = str_in_key.replace('"','')
str_in_key = str_in_key.replace("'",'')
# 正則過(guò)濾無(wú)用key,只保留key第一位為字母數(shù)據(jù)獲取[]_
str_sign = re.search('[^a-zA-Z0-9\_\[\]+]', str_in_key[0])
if str_sign is None:
# 處理value中的空格與轉(zhuǎn)義符
str_in_value = i[colon_str_index + 1:].strip()
str_in_value=str_in_value.replace('\\','')
try:
# 遇到是object類型的數(shù)據(jù)轉(zhuǎn)換一下
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("參數(shù)都沒(méi)有,還處理個(gè)球嘛")
return None
3.請(qǐng)求時(shí)將headers的content設(shè)置為m.content_type,會(huì)設(shè)置headers的content_type為form—data,類型為str:
MultipartEncoder相關(guān)源碼:


4.請(qǐng)求時(shí)設(shè)置data為m,會(huì)輸出一個(gè)MultipartEncoder對(duì)象:

方法2:
直接使用requests,無(wú)需依賴requests_toolbelt庫(kù)
過(guò)程大同小異,也是需要將字典的value轉(zhuǎn)換為str
注意:headers不要傳content_type字段,headers不要傳content_type字段,headers不要傳content_type字段
請(qǐng)求時(shí):data對(duì)應(yīng)附加參數(shù),files對(duì)應(yīng)files對(duì)象
#相關(guān)代碼
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())
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python使用百度API上傳文件到百度網(wǎng)盤(pán)代碼分享
- Python 一鍵獲取百度網(wǎng)盤(pán)提取碼的方法
- Python requests上傳文件實(shí)現(xiàn)步驟
- 基于python實(shí)現(xiàn)上傳文件到OSS代碼實(shí)例
- Python tornado上傳文件的功能
- python實(shí)現(xiàn)FTP循環(huán)上傳文件
- python實(shí)現(xiàn)上傳文件到linux指定目錄的方法
- Python調(diào)用scp向服務(wù)器上傳文件示例
- Python SELENIUM上傳文件或圖片實(shí)現(xiàn)過(guò)程
- python 實(shí)現(xiàn)百度網(wǎng)盤(pán)非會(huì)員上傳超過(guò)500個(gè)文件的方法
相關(guān)文章
利用LyScript實(shí)現(xiàn)應(yīng)用層鉤子掃描器
Capstone 是一個(gè)輕量級(jí)的多平臺(tái)、多架構(gòu)的反匯編框架。本篇文章將運(yùn)用LyScript插件結(jié)合Capstone反匯編引擎實(shí)現(xiàn)一個(gè)鉤子掃描器,感興趣的可以了解一下2022-08-08
基于Python采集爬取微信公眾號(hào)歷史數(shù)據(jù)
這篇文章主要介紹了基于Python采集爬取微信公眾號(hào)歷史數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Python3 串口接收與發(fā)送16進(jìn)制數(shù)據(jù)包的實(shí)例
今天小編就為大家分享一篇Python3 串口接收與發(fā)送16進(jìn)制數(shù)據(jù)包的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
在Python中編寫(xiě)數(shù)據(jù)庫(kù)模塊的教程
這篇文章主要介紹了在Python中編寫(xiě)數(shù)據(jù)庫(kù)模塊的教程,本文代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
使用Python創(chuàng)建LNK文件選擇器并導(dǎo)出配置文件
在這篇博客中,我將介紹如何使用Python的wxPython庫(kù)開(kāi)發(fā)一個(gè)GUI應(yīng)用程序,該應(yīng)用程序可以選擇文件夾中的.lnk(快捷方式)文件,并將選中的文件導(dǎo)出為特定格式的buttons.ini配置文件,需要的朋友可以參考下2025-01-01
python實(shí)現(xiàn)跨進(jìn)程(跨py文件)通信示例
本文主要介紹了python實(shí)現(xiàn)跨進(jìn)程(跨py文件)通信示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
解決pycharm同一目錄下無(wú)法import其他文件
今天小編就為大家分享一篇解決pycharm同一目錄下無(wú)法import其他文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python實(shí)現(xiàn)簡(jiǎn)易的圖書(shū)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)易的圖書(shū)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

