Python-Requests练习(一)
python requests包(接口自动化)
想看看requests包的东西,主要是为了接口自动化
安装不谈,直接pip install requests
应用安装
有个httpbin
网站可以用来练习requests的请求:
不过为了防止IP因为过多请求被封,还是本地搭建一个吧
这边使用docker安装:
1 | 安装: |
成功之后,浏览器就可以访问:http://192.168.0.126:8088/
快速入门
最简单的用法:
GET
1 | import requests |
返回
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "0",
"Host": "192.168.0.126:8088",
"User-Agent": "python-requests/2.21.0"
},
"json": null,
"origin": "192.168.11.144",
"url": "http://192.168.0.126:8088/put"
}
1
2
3
#### POST
r = requests.post("http://192.168.0.126:8088/post")
print(r.text)
1
2
3
#### PUT
r = requests.put("http://192.168.0.126:8088/put")
print(r.text)
1
2
3
#### DELETE
r = requests.delete("http://192.168.0.126:8088/delete")
print(r.text)
1
2
3
#### PATCH
r = requests.patch("http://192.168.0.126:8088/patch")
print(r.text)
```