1. 程式人生 > >requests--基於http協議的網絡庫

requests--基於http協議的網絡庫

file http 導包 協議 bsp 發送 param post 圖片

測試網站:http://httpbin.org

http協議兩大請求:get/post

導包:

import requests

import json

例子:

#不帶參get請求
r=requests.get("http://httpbin.org/get")

#帶參get請求
myParams={"username":"test","password":"12345"}
r=requests.get("http://httpbin.org/get",params=myParams)

#不帶參post請求
r=requests.post("http://httpbin.org/post")

#帶參post請求
myData={"username":"test","password":"123456"}
r=requests.post("http://httpbin.org/post",data=myData)

#提交json數據的post請求
myData={"username":"test","password":"123456"}
r=requests.post("http://httpbin.org/post",data=json.dumps(myData))

#通過post請求提交圖片
myfile={"file1":open("C:\\Users\\clareliu\\Desktop\\1.png","rb")}
r=requests.post("http://httpbin.org/post",files=myfile)

#建立會話後,通過會話發送get/post請求
s=requests.Session()
#get請求
r=s.get("http://httpbin.org/get")
#post請求
r=s.post("http://httpbin.org/post")

requests--基於http協議的網絡庫