前言
文件上傳在我們軟件是不可少的,最多的使用是體現在我們后台,當然我們前台也會有。但是了解過怎樣上傳文件嗎?這篇我們以禪道文檔-創建文檔,上傳文件為例。
post請求中的:Content-Type:multipart/form-data 這種類型便是上傳文件。
一、環境安裝、抓包分析
1、pip install requests_toolbelt,post請求 multipart/form-data 類型
C:\Users\Administrator>pip install requests_toolbelt Requirement already satisfied: requests_toolbelt in d:\path_python\lib\site-packages (0.9.1) Requirement already satisfied: requests<3.0.0,>=2.0.1 in d:\path_python\lib\site-packages (from requests_toolbelt) (2.20.1) Requirement already satisfied: chardet<3.1.0,>=3.0.2 in d:\path_python\lib\site-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (3.0.4) Requirement already satisfied: certifi>=2017.4.17 in d:\path_python\lib\site-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (2019.6.16) Requirement already satisfied: idna<2.8,>=2.5 in d:\path_python\lib\site-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (2.7) Requirement already satisfied: urllib3<1.25,>=1.21.1 in d:\path_python\lib\site-packages (from requests<3.0.0,>=2.0.1->requests_toolbelt) (1.24.3)
2、先抓下登錄接口,因為先登錄成功后才能上傳文件。寫上登錄代碼,讓其能正常登錄。
import requests s = requests.session() login_url = 'http://127.0.0.1/zentao/user-login.html' # 登錄url data = 'account=admin&password=e10adc3949ba59abbe56e057f20f883e&keepLogin%5B%5D=on&referer=http%3A%2F%2F127.0.0.1%2Fzentao%2Fdoc-browse-1-byModule-0-id_desc-doc.html' login_r = s.post(login_url, params=data) # 傳 params 參數 r1 = s.get('http://127.0.0.1/zentao/doc-browse-1-byModule-0-id_desc-doc.html') # 登錄訪問的 html print(r1.content.decode('utf-8'))
2、操作上傳文件(文件我就上傳一個桌面的:test.png),fiddler 切換至WebForms就看得比較清楚了。
二、寫python代碼請求
1、將抓包的接口寫下,順序:調試登錄、上傳、檢查點。
2、也需導入模塊from requests_toolbelt import MultipartEncoder
3、MultipartEncoder 傳的參數注意,特別是圖片參數,這里list里面元組:fields=[(name,value), (name2, value)]
4、禪道文件上傳代碼參考如下:
from requests_toolbelt import MultipartEncoder import requests # 登錄 s = requests.session() login_url = 'http://127.0.0.1/zentao/user-login.html' data = 'account=admin&password=e10adc3949ba59abbe56e057f20f883e&keepLogin%5B%5D=on&referer=http%3A%2F%2F127.0.0.1%2Fzentao%2Fdoc-browse-1-byModule-0-id_desc-doc.html' login_r = s.post(login_url, params=data) # 傳 params 參數 # 斷言是否登錄成功 r1 = s.get('http://127.0.0.1/zentao/doc-browse-1-byModule-0-id_desc-doc.html') if '產品主庫' in r1.content.decode('utf-8'): print('登錄成功') else:print('登錄失敗') # 文件上傳保存 # 傳參數,name 對應 value,注意圖片路徑的填寫。為空的一些參數不影響的去前提下可以刪除。 s_url = 'http://127.0.0.1/zentao/doc-create-1-0.html' body = MultipartEncoder( fields=[ ('lib', '1'), ('uid', '5dec436e32b85'), ('module', '0'), ('title', '我的一個title'), ('type', 'text'), ('contentType', 'html'), ('files[]', ('test.png', open('C:\\Users\\Administrator\\Desktop\\test.png', 'rb'), 'image/png')) ]) r2 = s.post(s_url, data=body, headers={'Content-Type': body.content_type}) # Content-Type 自動獲取 # 檢查是否成功上傳圖片 res = s.get('http://127.0.0.1/zentao/doc-browse-1.html') if '我的一個title' in res.content.decode('utf-8'): print('已上傳成功') else:print('上傳失敗')
5、python控制台說“已上傳成功”,但是我們首次執行最好還是肉眼看一下有沒有問題。(下圖顯然沒有問題)
但說到這里,不得不說這只是一次的測試請求,但是以后迭代,無數次回歸咋辦呢?這個問題和我們手工操作是一樣的,好比如你想title每次都一樣,但是只能存在一條數據。
那么就可以①邏輯刪除:操作刪除按鈕、物理刪除:連接數據庫執行刪除語句。歡迎來QQ交流群:482713805