
- Requests 教程
- Requests - 主頁
- Requests - 概述
- Requests - 環境設定
- Requests - HTTP 請求的工作原理
- Requests - 使用 Requests
- 處理 HTTP 請求的響應
- Requests - HTTP 請求標頭
- Requests - 處理 GET 請求
- 處理 POST、PUT、PATCH 和 DELETE 請求
- 請求 - 檔案上傳
- Requests - 使用 cookie
- Requests - 處理錯誤
- Requests - 處理超時
- Requests - 處理重定向
- Requests - 處理歷史記錄
- Requests - 處理會話
- Requests - SSL 證書
- Requests - 驗證
- Requests - 事件鉤子
- Requests - 代理
- Requests - 使用 Requests 進行網路抓取
- Requests 有用資源
- Requests - 快速指南
- Requests - 有用資源
- Requests - 討論
請求 - 檔案上傳
在本章中,我們將使用 request 上傳檔案並讀取上傳的檔案的內容。我們可以按如下示例所示使用 files 引數執行此操作。
我們將使用 http://httpbin.org/post 上傳檔案。
示例
import requests myurl = 'https://httpbin.org/post' files = {'file': open('test.txt', 'rb')} getdata = requests.post(myurl, files=files) print(getdata.text)
Test.txt
File upload test using Requests
輸出
E:\prequests>python makeRequest.py { "args": {}, "data": "", "files": { "file": "File upload test using Requests" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "175", "Content-Type": "multipart/form-data; boundary=28aee3a9d15a3571fb80d4d2a94bfd33", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0" }, "json": null, "origin": "117.223.63.135, 117.223.63.135", "url": "https://httpbin.org/post" }
也可以像下面那樣傳送檔案的內容:
示例
import requests myurl = 'https://httpbin.org/post' files = {'file': ('test1.txt', 'Welcome to TutorialsPoint')} getdata = requests.post(myurl, files=files) print(getdata.text)
輸出
E:\prequests>python makeRequest.py { "args": {}, "data": "", "files": { "file": "Welcome to TutorialsPoint" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "170", "Content-Type": "multipart/form-data; boundary=f2837238286fe40e32080aa7e172be4f", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0" }, "json": null, "origin": "117.223.63.135, 117.223.63.135", "url": "https://httpbin.org/post" }
廣告