python筆記8:requests模組

PHP中文网
發布: 2017-07-09 18:13:09
原創
1531 人瀏覽過

簡介:

requests是python的一個http客戶端函式庫,跟urlib、urlib2類似,requests模組是基於urlib模組開發的,程式碼實作更為簡潔。

模組安裝:

pip install requests ,使用requests模組時直接import requests即可。

簡單使用requests:

http協定的接口,請求方式分為get和post,這2種最為常用。請求參數常用的形式有key-value、json、檔案上傳、介面新增cookies、headers等操作。

get請求-url拼接參數:

<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>
登入後複製

get請求- 請求參數類型是key-value形式:

<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>
登入後複製
get請求呼叫的介面實作如下:
#
<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)
登入後複製
View Code

post請求 - url拼接參數

<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()
登入後複製

post請求 - 傳參類型是key-value形式

<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()
登入後複製
post請求呼叫的介面實作如下:
#
<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)
登入後複製
View Code

 post請求- 傳參類型是json

<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)
登入後複製

 post請求- 傳參類型是json,多層json嵌套

##
<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()
登入後複製
post請求呼叫的接口,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)
登入後複製
View Code

post請求 - 上傳檔案
<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)
登入後複製
post請求呼叫的上傳接口,程式碼實作如下:
#
<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)
登入後複製
View Code

post請求 - 新增cookies
###
<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)
登入後複製
###
post请求调用的添加cookie接口,代码实现如下:
<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)
登入後複製
View Code

 post请求- 添加headers

<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中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板