python3--requests

2022-10-31 12:46:35

requests是一个简单的python 的http库.本文档整理了一些简单的用法。cookies,重定向,以及一些高级用法没有整理

安装

pip install requests

请求参数

参数用途备注
paramsget请求传递参数字典,字典的value为列表
datapost请求传递参数字典,元组列表,字典的value为列表
headers传递头信息,字典形式。类型为str,字节字符串或unicode
jsonpost请求使用,传递未编码的参数json如果传递了data或参数,则将忽略该参数files
files上传多部分编码的文件
timeout指定的秒数后停止等待响应

reques请求响应

r= requests.get('http://10.130.25.80:1122/get_test', params={'key_get': 'value_get'})

操作含义备注
requests.get('http://10.130.25.80:1122/get_test')发起请求同时也可发起其他类型的请求
r.urlrequest对象的URL
r.text展示响应内容,编码为请求根据头文件猜测的编码
r.content展示响应内容,类型为字节类型,即二进制响应内容一般是图像,文件的时候需要以这种方式拿响应内容
r.json()获取json响应内容当响应内容不是json时,将有对应的报错ValueError: No JSON object could be decoded
r.raw原始的字节流仅记录,不知道获取的是什么数据,没有测试出来
r.encoding查询正在使用的编码修改编码可使用r.encoding='utf-8'
r.status_code返回状态码
r.headers查看服务器的响应headers格式为字典,可通过key获取value的值
r.request.hearder查看客户端发送为服务器的header
r.cookies获取cookiesCookie以RequestsCookieJar的形式返回

案例:

发起请求

r_get= requests.get('http://10.130.25.80:1122/get_test')

发起带参数的请求

带参数的get请求

如: 请求为:http://10.130.25.80:1122/get_test?key_get=value_get

r_get= requests.get('http://10.130.25.80:1122/get_test', params={'key_get':'value_get'})print(r_get.url)

请求结果:

http://10.130.25.80:1122/get_test?key_get=value_get

也可以以列表形式传递参数的值

请求:http://10.130.25.80:1122/get_test?key_get=value_get&key_get_list_2=value_list_0&key_get_list_2=value_list_1

r_get1= requests.get('http://10.130.25.80:1122/get_test', params={'key_get':'value_get','key_get_list_2':['value_list_0','value_list_1']})print(r_get1.url)

带参数的post请求

data传递参数,字典类型

r_post= requests.post('http://10.130.25.80:1122/qnWebCallBack', data={'key':'value'})print(r_post.url)print(r_post.text)

data传递参数,参数为元组列表或字典的value为列表

body为: key=value&key=value2

#data为元组列表
r_post= requests.post('http://10.130.25.80:1122/qnWebCallBack', data=[('key','value'),('key','value2')])print(r_post.url)print(r_post.text)#data的字典的value为列表
r_post_1= requests.post('http://10.130.25.80:1122/qnWebCallBack', data={'key':['value','value2']})print(r_post.url)print(r_post.text)

传递未编码的数据

# json 发送未经格式编码的数据
r_post= requests.post('http://10.130.25.80:1122/qnWebCallBack', json={'key':'value'})print(r_post.url)print(r_post.text)# print(r_post.headers)# 转化为json
data={'key':'value'}
r_post_1= requests.post('http://localhost:1122/qnWebCallBack', data=json.dumps(data))print(r_post_1.url)print(r_post_1.text)

传递多部分编码的文件

  • 传递一个文件
#传递一个文件
r_post= requests.post('http://localhost:1122/test', files={'file':('sql.txt',open('sql.txt','rb'))})print(r_post.text)

响应

--dfcfda2909a3f1f77b16e1ba3f4621db
Content-Disposition: form-data; name="file"; filename="sql.txt"

select*from mysql--dfcfda2909a3f1f77b16e1ba3f4621db--
  • 传递一个文件,显示声明header
#传递一个文件,显示声明header
r_post= requests.post('http://localhost:1122/test', files={'file':('sql.txt',open('sql.txt','rb',),'application/json;charset=utf-8')})print(r_post.text)

响应

--71833f4808a013a3253b578dae8d03c5
Content-Disposition: form-data; name="file"; filename="sql.txt"
Content-Type: application/json;charset=utf-8

select*from mysql--71833f4808a013a3253b578dae8d03c5--
  • 传递一个文件,传递字符串作为文件接收
#传递一个文件,传递字符串作为文件接收
r_post= requests.post('http://localhost:1122/test', files={'file':('sql.txt','I am string')})print(r_post.text)

响应

--52ef8094b133915b17d9ebc748f40ff6
Content-Disposition: form-data; name="file"; filename="sql.txt"

I am string--52ef8094b133915b17d9ebc748f40ff6--

查看响应内容

r_get= requests.get('http://10.130.25.80:1122/get_test', params={'key_get':'value_get'})print(r_get.text)print(r_get.content)print(r_get.json())

响应:

接收成功,数据为:
b'\xe6\x8e\xa5\xe6\x94\xb6\xe6\x88\x90\xe5\x8a\x9f\xef\xbc\x8c\xe6\x95\xb0\xe6\x8d\xae\xe4\xb8\xba\xef\xbc\x9a'
json.decoder.JSONDecodeError: Expecting value: line1 column1(char0)
  • text根据猜想的编码显示,显示的是文本内容
  • content显示的响应为字节类型的,也就是二进制
  • json()获取json响应内容

查看响应状态码

r_get= requests.get('http://10.130.25.80:1122/get_test', params={'key_get':'value_get'})print(r_get.status_code)

响应:

200

查看服务的header

r_post= requests.post('http://localhost:1122/test', files={'file':('sql.txt',open('sql.txt','rb',),'application/json;charset=utf-8')})# 获取headersprint(r_post.headers)# 根据key获取deadersprint(r_post.headers['Content-Type'])print(r_post.headers.get("Content-Type"))

响应

{'Date':'Wed, 03 Feb 2021 08:49:41 GMT','Server':'WSGIServer/0.2 CPython/3.8.4','Content-Type':'text/html; charset=UTF-8'}
text/html; charset=UTF-8
text/html; charset=UTF-8

获取cookies

r_get= requests.get('http://10.130.25.120:7001/dcms')print(r_get.cookies)

响应

<RequestsCookieJar[<Cookie JSESSIONID=kpNKghshJJBsJLTpqGpP4W7pTL6DfPNtQTVHTkhSJ2mw5StQC2Pv!1963428134for10.130.25.120/>]>

超时

#服务器,延迟2秒返回结果
@route('/test', method='get')defqnWebCallBack():print(type(request.body))
    time.sleep(2)return request.body#客户端,仅等待1s# 测试延时返回
r_get= requests.get('http://localhost:1122/test',timeout=1)

响应

socket.timeout: timed outraise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='localhost', port=1122): Read timed out.(read timeout=1)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='localhost', port=1122): Read timed out.(read timeout=1)

参考链接:
https://www.cnblogs.com/zhangxinqi/p/9201594.html
https://3.python-requests.org/user/quickstart/

  • 作者:jjt_!!
  • 原文链接:https://blog.csdn.net/jjt_zaj/article/details/113525869
    更新时间:2022-10-31 12:46:35