1. 程式人生 > >#上傳檔案的post請求

#上傳檔案的post請求

#上傳檔案的post請求
#files={"files":open("test.txt","rb")}#普通上傳
#files={"files":{"巧吧軟體測試.jpg",open("巧吧軟體測試.jpg","rb"),"image/jpg",{"refer":"localhost"}}}#設定檔名、檔案型別和請求頭
#files={{"field1",{"test.txt",open("test.txt","rb")}},}

import requests
url = "https://httpbin.org/post"

#第一種:普通上傳
#files = {"files":open("test.txt","rb")}

#第二種:通過檔案上傳字串等
#files = {"files":("test.txt","send hongtao")}

#第三種:自定義檔名、檔案型別以及請求頭(請求檔名稱、檔案路徑、檔案型別、檔案請求頭)
#files = {"files":open("巧吧軟體測試.png","rb")}
#files = {"files":("巧吧軟體測試.png",open("巧吧軟體測試.png","rb"),"image/png")}

#第四種:傳送多個檔案
#files = [("field1",("test.txt",open("test.txt","rb"))),
# ("field2",("巧吧軟體測試.png",open("巧吧軟體測試.png","rb"),"image/png"))]

#r = requests.post(url,files=files)

#第五種:流式上傳
with open("test.txt") as f:
r = requests.post(url,data=f)

print(r.headers)
print(r.text)