requests 检测请求
http://httpbin.org 可以帮我们测试我们用 requests库发送出去了什么请求
如图所示,我们可以看到非常多让我们测试的方法和请求,
httpmethods:可以帮我测试各种不同的http方法,如果你想测试get方法,请求到路径就可以。
测试get方法:
import requests as re url='https://httpbin.org/get' res=re.get(url) print(res.text)
输出结果:输出的结果,就是我们发送给服务器的请求,这样就可以看到我们发送了什么给服务器。
{
"args": {}, #自带的参数
"headers": { #headers是python帮我们自动带上的。
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.5",
"X-Amzn-Trace-Id": "Root=1-68cf4bd7-5d4e8bc569ae7eec186d7425"
},
"origin": "183.229.35.169", #就是发送请求电脑的IP
"url": " #url就是发送到那个网址。
}args:{}这部分参数我们在网址中有看到过,括号里面有可能,https://httpbin.org/get?page=2&count=5&huiyuan=no
这种网址就是在传递参数,
如果我们发送的网址附带参数,我们要这样写,可以增加一个字典。
import requests as re
url='https://httpbin.org/get'
params={
'page':'2',
'count':'5'
}
res=re.get(url,params=params)
print(res.text)输出结果:把我们想要的参数加入进去了
{
"args": {
"count": "5",
"page": "2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.5",
"X-Amzn-Trace-Id": "Root=1-68cf507e-779feb693aec321a483ebed5"
},
"origin": "183.229.35.169",
"url": "https://httpbin.org/get?page=2&count=5"
}测试post方法:
import requests as re
url='https://httpbin.org/post'
params={
'page':'2',
'count':'5'
}
res=re.post(url,params=params)
print(res.text)输出结果:跟刚刚不一样,多了data、多了files、多了form、下面多了一个josn,由于post,主要上传资料到服务器
{
"args": {
"count": "5",
"page": "2"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.5",
"X-Amzn-Trace-Id": "Root=1-68cf60c7-196e8cb259d121d570e59bd1"
},
"json": null,
"origin": "183.229.35.169",
"url": "https://httpbin.org/post?page=2&count=5"
}所以我们多上传一个上传的资料。data格式上传
import requests as re
url='https://httpbin.org/post'
params={
'page':'2',
'count':'5'
}
data={
' name':'nan',
'age':'23'
}
res=re.post(url,params=params,data=data)
print(res.text)输出结果:可以看到,我们填写的资料,上传到from里面了,我们资料用表单形式上传上去了。
{
"args": {
"count": "5",
"page": "2"
},
"data": "",
"files": {},
"form": {
" name": "nan",
"age": "23"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Length": "16",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.5",
"X-Amzn-Trace-Id": "Root=1-68cf61c9-7a3dcfc10c21e4a14290ca99"
},
"json": null,
"origin": "183.229.35.169",
"url": "https://httpbin.org/post?page=2&count=5"
}JSON格式上传
import requests as re
url='https://httpbin.org/post'
params={
'page':'2',
'count':'5'
}
data={
' name':'nan',
'age':'23'
}
res=re.post(url,params=params,json=data)
print(res.text)file格式上传,图片上传。
import requests as re
url='https://httpbin.org/post'
params={
'page':'2',
'count':'5'
}
data={
' name':'nan',
'age':'23'
}
with open('123.jpg', mode='rb') as file:
image = {'upload_image': file.read()}
r = re.post(url, params=params, data=image)
print(r.text)输出结果:
D:\anaconda3\envs\myfastapi\python.exe D:\workbench\myfastapi\requ.py
{
"args": {
"count": "5",
"page": "2" },
"data": "",
"files": {},
"form": {
"upload_image": "\u0018Exif\u0000\u0000II*\u0000\b\u0000\u0000\u0000\u0000\u0000\" },
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Length": "22719",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.32.5",
"X-Amzn-Trace-Id": "Root=1-68cf6a76-12a83d7b4b4310b30b9859dd" },
"json": null,
"origin": "183.229.35.169",
"url": "https://httpbin.org/post?page=2&count=5"}修改 User-Agent和headers ,伪装成别的。
"User-Agent": "python-requests/2.32.5", 这句话就是请求是从python程序发送的,不是人,太诚实了,所以伪装下。
import requests as re
url='https://httpbin.org/post'
#把浏览器数值存在header变量里。
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36'}
params={
'page':'2',
'count':'5'
}
data={
' name':'nan',
'age':'23'
}
#with open('123.jpg', mode='rb') as file:
# image = {'upload_image': file.read()}
r = re.post(url, params=params, headers=header)
print(r.text)输出结果:浏览器User-Agent伪装成功
{
"args": {
"count": "5",
"page": "2"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-68cf65ab-75737943594f3fcb15e0e029"
},
"json": null,
"origin": "183.229.35.169",
"url": "https://httpbin.org/post?page=2&count=5"
}