requests是python的一个http客户端库,跟urlib、urlib2类似,requests模块是基于urlib模块开发的,代码实现更为简洁。
pip install requests ,使用requests模块时直接import requests即可。
http协议的接口,请求方式分为get和post,这2种最为常用。请求参数常用的形式有key-value、json、文件上传、接口添加cookies、headers等操作。
<span style="color: #0000ff">import</span><span style="color: #000000"> requests url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/gettest?username=abc</span><span style="color: #800000">'</span> <span style="color: #008000">#</span><span style="color: #008000">返回结果类型是字符串:{"msg": "pass"}</span> res =<span style="color: #000000"> requests.<span style="color: #ff0000">get</span>(url).text</span><span style="color: #0000ff"><br></span>
也可以使用.json(),返回结果类型是字典 (注:接口请求的返回值类型 与接口的返回定义类型有关)
<span style="color: #008000">#</span><span style="color: #008000">返回结果类型是字典:{'msg': 'pass'}</span> res_dic =<span style="color: #000000"> requests.<span style="color: #ff0000">get</span>(url).json()</span><span style="color: #0000ff"><br></span>
<span style="color: #0000ff">import</span><span style="color: #000000"> requests url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/gettest</span><span style="color: #800000">'</span> <span style="color: #008000">#</span><span style="color: #008000">请传参类型是 name=xxx&pwd=123456</span> data = {<span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">abc</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008000">#</span><span style="color: #008000">返回结果类型是str:{"msg": "pass"}</span> res = requests.<span style="color: #ff0000">get</span>(url, data).text
也可以使用.json(),返回结果类型是字典 (注:接口请求的返回值类型 与接口的返回定义类型有关)
<span style="color: #008000">#</span><span style="color: #008000">返回结果类型是字典:{"msg": "pass"}</span> res_dic =<span style="color: #000000"> requests.<span style="color: #ff0000">get</span>(url, data).json()</span><span style="color: #0000ff"><br></span>
<span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> flask </span><span style="color: #008080"> 2</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> request </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> jsonify </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> server = flask.Flask(<span style="color: #800080">__name__</span><span style="color: #000000">) </span><span style="color: #008080"> 6</span> @server.route(<span style="color: #800000">'</span><span style="color: #800000">/gettest</span><span style="color: #800000">'</span>, methods=[<span style="color: #800000">'</span><span style="color: #800000">get</span><span style="color: #800000">'</span><span style="color: #000000">]) </span><span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span><span style="color: #000000"> test1(): </span><span style="color: #008080"> 8</span> name = request.values.get(<span style="color: #800000">'</span><span style="color: #800000">username</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080"> 9</span> <span style="color: #0000ff">if</span> name == <span style="color: #800000">'</span><span style="color: #800000">abc</span><span style="color: #800000">'</span><span style="color: #000000">: </span><span style="color: #008080">10</span> <span style="color: #008000">#</span><span style="color: #008000">接口返回值类型为字典</span> <span style="color: #008080">11</span> res = {<span style="color: #800000">"</span><span style="color: #800000">msg</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">pass</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008080">12</span> <span style="color: #0000ff">return</span><span style="color: #000000"> jsonify(res) </span><span style="color: #008080">13</span> <span style="color: #0000ff">else</span><span style="color: #000000">: </span><span style="color: #008080">14</span> res = {<span style="color: #800000">'</span><span style="color: #800000">msg</span><span style="color: #800000">'</span>: <span style="color: #800000">"</span><span style="color: #800000">fail</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008080">15</span> <span style="color: #0000ff">return</span><span style="color: #000000"> jsonify(res) </span><span style="color: #008080">16</span> server.run(debug=True)
<span style="color: #0000ff">import</span><span style="color: #000000"> requests </span><span style="color: #0000ff">import</span><span style="color: #000000"> json url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/gettest?username=abc</span><span style="color: #800000">'</span> <span style="color: #008000">#</span><span style="color: #008000">返回结果类型是str:{"msg": "pass"}</span> res =<span style="color: #000000"> requests.<span style="color: #ff0000">post</span>(url).text </span><span style="color: #008000">#</span><span style="color: #008000">可以通过json.loads将字符串(json串)转换为字典类型,方便取值操作</span> res_dic = json.loads(res)
也可以使用.json(),返回结果类型是字典 (注:接口请求的返回值类型 与接口的返回定义类型有关)
<span style="color: #008000">#</span><span style="color: #008000">返回结果类型为字典:{'msg': 'pass'},字典取值操作比较方法</span> res_dic = requests.<span style="color: #ff0000">post</span>(url).json()
<span style="color: #0000ff">import</span><span style="color: #000000"> requests </span><span style="color: #0000ff">import</span><span style="color: #000000"> json url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/gettest?username=abc</span><span style="color: #800000">'</span><span style="color: #000000"> data </span>= {<span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">abc</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008000">#</span><span style="color: #008000">返回结果类型是str:{"msg": "pass"}</span> res =<span style="color: #000000"> requests.<span style="color: #ff0000">post</span>(url, data).text </span><span style="color: #008000">#</span><span style="color: #008000">可以通过json.loads将字符串(json串)转换为字典类型,方便取值操作</span> res_dic = json.loads(res)
也可以使用.json(),返回结果类型是字典 (注:接口请求的返回值类型 与接口的返回定义类型有关)
<span style="color: #008000">#</span><span style="color: #008000">返回结果类型为字典:{'msg': 'pass'},字典取值操作比较方法</span> res_dic = requests.<span style="color: #ff0000">post</span>(url, data).json()
<span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> flask </span><span style="color: #008080"> 2</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> request </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> jsonify </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> server = flask.Flask(<span style="color: #800080">__name__</span><span style="color: #000000">) </span><span style="color: #008080"> 6</span> @server.route(<span style="color: #800000">'</span><span style="color: #800000">/gettest</span><span style="color: #800000">'</span>, methods=[<span style="color: #800000">'</span><span style="color: #800000">post</span><span style="color: #800000">'</span><span style="color: #000000">]) </span><span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span><span style="color: #000000"> test1(): </span><span style="color: #008080"> 8</span> <span style="color: #008000">#</span><span style="color: #008000">request.values.get('xxx') 这种获取请求参数的方式,可以通过url拼接参数和key-value形式访问接口。</span> <span style="color: #008080"> 9</span> name = request.values.get(<span style="color: #800000">'</span><span style="color: #800000">username</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">10</span> <span style="color: #0000ff">if</span> name == <span style="color: #800000">'</span><span style="color: #800000">abc</span><span style="color: #800000">'</span><span style="color: #000000">: </span><span style="color: #008080">11</span> <span style="color: #008000">#</span><span style="color: #008000">接口返回值类型为字典</span> <span style="color: #008080">12</span> res = {<span style="color: #800000">"</span><span style="color: #800000">msg</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">pass</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008080">13</span> <span style="color: #0000ff">return</span><span style="color: #000000"> jsonify(res) </span><span style="color: #008080">14</span> <span style="color: #0000ff">else</span><span style="color: #000000">: </span><span style="color: #008080">15</span> res = {<span style="color: #800000">'</span><span style="color: #800000">msg</span><span style="color: #800000">'</span>: <span style="color: #800000">"</span><span style="color: #800000">fail</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008080">16</span> <span style="color: #0000ff">return</span><span style="color: #000000"> jsonify(res) </span><span style="color: #008080">17</span> server.run(debug=True)
<span style="color: #0000ff">import</span><span style="color: #000000"> requests url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/register</span><span style="color: #800000">'</span><span style="color: #000000"> data </span>=<span style="color: #000000"> { </span><span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">admin</span><span style="color: #800000">"</span><span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">pwd</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">123456</span><span style="color: #800000">"</span><span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">c_pwd</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">123456</span><span style="color: #800000">"</span><span style="color: #000000"> } </span><span style="color: #008000">#</span><span style="color: #008000">json类型传参,post接口请求时,需要指明下请求参数是json类型,返回结果是字典:{'msg': 'ok'}</span> res = requests.<span style="color: #ff0000">post</span>(url, <span style="color: #ff0000">json</span>=<span style="color: #000000">data).json() </span><span style="color: #0000ff">print</span>(type(res), res)
<span style="color: #0000ff">import</span><span style="color: #000000"> requests url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/register</span><span style="color: #800000">'</span><span style="color: #000000"> data </span>=<span style="color: #000000"> { </span><span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">admin</span><span style="color: #800000">"</span><span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">pwd</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">123456</span><span style="color: #800000">"</span><span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">c_pwd</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">123456</span><span style="color: #800000">"</span><span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">items</span><span style="color: #800000">"</span><span style="color: #000000">: { </span><span style="color: #800000">"</span><span style="color: #800000">id</span><span style="color: #800000">"</span>: 1<span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">age</span><span style="color: #800000">"</span>: 18<span style="color: #000000">, </span><span style="color: #800000">"</span><span style="color: #800000">sex</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">man</span><span style="color: #800000">"</span><span style="color: #000000"> } } </span><span style="color: #008000">#</span><span style="color: #008000">json类型传参,post接口请求时,需要指明下请求参数是json类型,返回结果是字典:{'msg': 'ok'}</span> res = requests.<span style="color: #ff0000">post</span>(url, <span style="color: #ff0000">json</span>=data).json()
<span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> flask </span><span style="color: #008080"> 2</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> request </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> jsonify </span><span style="color: #008080"> 4</span> <span style="color: #008080"> 5</span> server = flask.Flask(<span style="color: #800080">__name__</span><span style="color: #000000">) </span><span style="color: #008080"> 6</span> @server.route(<span style="color: #800000">'</span><span style="color: #800000">/register</span><span style="color: #800000">'</span>, methods=[<span style="color: #800000">'</span><span style="color: #800000">post</span><span style="color: #800000">'</span><span style="color: #000000">]) </span><span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span><span style="color: #000000"> test1(): </span><span style="color: #008080"> 8</span> <span style="color: #008000">#</span><span style="color: #008000">request.json,传参类型为json,返回结果类型为字典:{'pwd': '123456', 'username': 'asdf', 'c_pwd': '123456'}</span> <span style="color: #008080"> 9</span> res_dic =<span style="color: #000000"> request.json </span><span style="color: #008080">10</span> username = res_dic.get(<span style="color: #800000">'</span><span style="color: #800000">username</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">11</span> pwd = res_dic.get(<span style="color: #800000">'</span><span style="color: #800000">pwd</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">12</span> c_pwd = res_dic.get(<span style="color: #800000">'</span><span style="color: #800000">c_pwd</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">13</span> <span style="color: #0000ff">if</span> username <span style="color: #0000ff">and</span> pwd <span style="color: #0000ff">and</span><span style="color: #000000"> c_pwd: </span><span style="color: #008080">14</span> <span style="color: #0000ff">if</span> res_dic.get(<span style="color: #800000">'</span><span style="color: #800000">username</span><span style="color: #800000">'</span>) == <span style="color: #800000">'</span><span style="color: #800000">admin</span><span style="color: #800000">'</span><span style="color: #000000">: </span><span style="color: #008080">15</span> <span style="color: #0000ff">if</span> res_dic.get(<span style="color: #800000">'</span><span style="color: #800000">pwd</span><span style="color: #800000">'</span>) == res_dic.get(<span style="color: #800000">'</span><span style="color: #800000">c_pwd</span><span style="color: #800000">'</span><span style="color: #000000">): </span><span style="color: #008080">16</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">'</span><span style="color: #800000">msg</span><span style="color: #800000">'</span>: <span style="color: #800000">"</span><span style="color: #800000">ok</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">17</span> <span style="color: #0000ff">else</span><span style="color: #000000">: </span><span style="color: #008080">18</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">'</span><span style="color: #800000">msg</span><span style="color: #800000">'</span>: <span style="color: #800000">"</span><span style="color: #800000">密码不一致</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">19</span> <span style="color: #0000ff">else</span><span style="color: #000000">: </span><span style="color: #008080">20</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">'</span><span style="color: #800000">msg</span><span style="color: #800000">'</span>: <span style="color: #800000">"</span><span style="color: #800000">username不能注册</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">21</span> server.run(debug=True)
<span style="color: #0000ff">import</span><span style="color: #000000"> requests url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/upload</span><span style="color: #800000">'</span> <span style="color: #008000">#</span><span style="color: #008000">上传接口请求数据,指定上传文件的路径</span> data = {<span style="color: #800000">"</span><span style="color: #800000">file_name</span><span style="color: #800000">"</span>: <span style="color: #ff0000">open('E:/python_workspace/base-code/asdf.txt', encoding='utf8'</span><span style="color: #000000"><span style="color: #ff0000">)</span>} </span><span style="color: #008000">#</span><span style="color: #008000">post请求,请求参数类型是file,返回结果类型是字典</span> res = requests.<span style="color: #ff0000">post</span>(url, <span style="color: #ff0000">files</span>=<span style="color: #000000">data).json() </span><span style="color: #0000ff">print</span>(type(res), res)
<span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> flask </span><span style="color: #008080"> 2</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> request </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> jsonify </span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time, os </span><span style="color: #008080"> 5</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> send_from_directory </span><span style="color: #008080"> 6</span> <span style="color: #008000">#</span><span style="color: #008000">上传文件接口</span> <span style="color: #008080"> 7</span> server = flask.Flask(<span style="color: #800080">__name__</span><span style="color: #000000">) </span><span style="color: #008080"> 8</span> @server.route(<span style="color: #800000">'</span><span style="color: #800000">/upload</span><span style="color: #800000">'</span>, methods=[<span style="color: #800000">'</span><span style="color: #800000">post</span><span style="color: #800000">'</span><span style="color: #000000">]) </span><span style="color: #008080"> 9</span> <span style="color: #0000ff">def</span><span style="color: #000000"> upload(): </span><span style="color: #008080">10</span> <span style="color: #008000">#</span><span style="color: #008000">获取请求参数,传参类型是file,返回结果类型是一个对象:<class 'werkzeug.datastructures.FileStorage'> <FileStorage: '新建文本文档.txt' ('text/plain')></span> <span style="color: #008080">11</span> file = request.files.get(<span style="color: #800000">'</span><span style="color: #800000">file_name</span><span style="color: #800000">'</span><span style="color: #000000">, None) </span><span style="color: #008080">12</span> <span style="color: #008000">#</span><span style="color: #008000">判断file是否为空,若为空则没有上传文件</span> <span style="color: #008080">13</span> <span style="color: #0000ff">if</span><span style="color: #000000"> file: </span><span style="color: #008080">14</span> cur_time = time.strftime(<span style="color: #800000">'</span><span style="color: #800000">%Y%m%d%H%M%S</span><span style="color: #800000">'</span>) <span style="color: #008000">#</span><span style="color: #008000">获取当前时间</span> <span style="color: #008080">15</span> upload_name = file.filename <span style="color: #008000">#</span><span style="color: #008000">获取上传文件的名称</span> <span style="color: #008080">16</span> new_file_name = cur_time+upload_name <span style="color: #008000">#</span><span style="color: #008000">给文件重命名,防止有重复文件覆盖</span> <span style="color: #008080">17</span> <span style="color: #008000">#</span><span style="color: #008000"> 保存文件,指定文件上传的路径</span> <span style="color: #008080">18</span> file.save(<span style="color: #800000">'</span><span style="color: #800000">E:/python_workspace/base-code/</span><span style="color: #800000">'</span>+<span style="color: #000000">new_file_name) </span><span style="color: #008080">19</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">"</span><span style="color: #800000">code</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">ok</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">20</span> <span style="color: #0000ff">else</span><span style="color: #000000">: </span><span style="color: #008080">21</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">"</span><span style="color: #800000">code</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">请上传文件</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">22</span> <span style="color: #008080">23</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">: </span><span style="color: #008080">24</span> server.run(debug=True)
<span style="color: #0000ff">import</span><span style="color: #000000"> requests url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/set_cookies</span><span style="color: #800000">'</span><span style="color: #000000"> data </span>= {<span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">xiaobai</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">monkey</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">999</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008000">#</span><span style="color: #008000">请求cookie参数</span> <span style="color: #ff0000">cookie</span> = {<span style="color: #800000">"</span><span style="color: #800000">token</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">123456</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008000">#</span><span style="color: #008000">接口请求,返回结果类型是字典:{'msg': 'ok'}</span> res = requests.<span style="color: #ff0000">post</span>(url, data=data, <span style="color: #ff0000">cookies</span>=<span style="color: #000000">cookie).json() </span><span style="color: #0000ff">print</span>(type(res), res)
<span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> flask </span><span style="color: #008080"> 2</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> request </span><span style="color: #008080"> 3</span> <span style="color: #0000ff">from</span> flask <span style="color: #0000ff">import</span><span style="color: #000000"> jsonify </span><span style="color: #008080"> 4</span> server = flask.Flask(<span style="color: #800080">__name__</span><span style="color: #000000">) </span><span style="color: #008080"> 5</span> @server.route(<span style="color: #800000">'</span><span style="color: #800000">/set_cookies</span><span style="color: #800000">'</span>, methods=[<span style="color: #800000">'</span><span style="color: #800000">post</span><span style="color: #800000">'</span><span style="color: #000000">]) </span><span style="color: #008080"> 6</span> <span style="color: #0000ff">def</span><span style="color: #000000"> set_cookies(): </span><span style="color: #008080"> 7</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">获取到的cookie:</span><span style="color: #800000">'</span>, request.cookies) <span style="color: #008000">#</span><span style="color: #008000">{'token': '123456'}</span> <span style="color: #008080"> 8</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">获取到的请求数据:</span><span style="color: #800000">'</span>, request.values) <span style="color: #008000">#</span><span style="color: #008000"> CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('monkey', '999'), ('username', 'xiaobai')])])</span> <span style="color: #008080"> 9</span> token = request.cookies.get(<span style="color: #800000">'</span><span style="color: #800000">token</span><span style="color: #800000">'</span><span style="color: #000000">) </span><span style="color: #008080">10</span> <span style="color: #0000ff">if</span><span style="color: #000000"> token: </span><span style="color: #008080">11</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">"</span><span style="color: #800000">msg</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">ok</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">12</span> <span style="color: #0000ff">else</span><span style="color: #000000">: </span><span style="color: #008080">13</span> <span style="color: #0000ff">return</span> jsonify({<span style="color: #800000">"</span><span style="color: #800000">msg</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">没有添加cookie</span><span style="color: #800000">"</span><span style="color: #000000">}) </span><span style="color: #008080">14</span> <span style="color: #008080">15</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">: </span><span style="color: #008080">16</span> server.run(debug=True)
<span style="color: #0000ff">import</span><span style="color: #000000"> requests header_url </span>= <span style="color: #800000">'</span><span style="color: #800000">http://127.0.0.1:5000/set_headers</span><span style="color: #800000">'</span><span style="color: #000000"> data </span>= {<span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">123456</span><span style="color: #800000">"</span>, <span style="color: #800000">"</span><span style="color: #800000">monkey</span><span style="color: #800000">"</span>: <span style="color: #800000">"</span><span style="color: #800000">666</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008000">#</span><span style="color: #008000">添加header信息</span> header = {<span style="color: #800000">'</span><span style="color: #800000">Content-Type</span><span style="color: #800000">'</span>: <span style="color: #800000">"</span><span style="color: #800000">application/json</span><span style="color: #800000">"</span><span style="color: #000000">} </span><span style="color: #008000">#</span><span style="color: #008000">接口请求</span> res = requests.<span style="color: #ff0000">post</span>(header_url, data, <span style="color: #ff0000">headers</span>=header).json()
欢迎评论~~~
以上是python笔记8:requests模块的详细内容。更多信息请关注PHP中文网其他相关文章!