search
HomeBackend DevelopmentPython Tutorialpython notes 8: requests module
python notes 8: requests moduleJul 09, 2017 pm 06:13 PM
pythonrequestsmodule

Introduction:

requests is an http client library for Python, similar to urlib and urlib2. The requests module is developed based on the urlib module, and the code implementation is simpler.

Module installation:

pip install requests, just import requests directly when using the requests module.

Simple use of requests:

http protocol interface, the request method is divided into get and post, these two are the most commonly used. Commonly used forms of request parameters include key-value, json, file upload, interface addition of cookies, headers and other operations.

get request-url splicing parameters:

<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>

You can also use .json(), the return result type is a dictionary (Note: The return value type of the interface request is related to the return definition type of the interface)

<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 request - the request parameter type is in key-value form:

<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

You can also use .json(), the return result type is a dictionary (Note: The return value type of the interface request is related to the return definition type of the interface)

<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>
The interface implementation called by the get request is as follows:
<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 request - url splicing parameters

<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)

You can also use .json(), the return result type is a dictionary (Note: The return value type of the interface request is related to the return definition type of the interface)

<span style="color: #008000">#</span><span style="color: #008000">返回结果类型为字典:{'msg': 'pass'},字典取值操作比较方法</span>
res_dic = requests.<span style="color: #ff0000">post</span>(url).json()

Post request - the parameter type is in key-value format

<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)

You can also use .json(), the return result type is a dictionary (Note: The return value type of the interface request is related to the return definition type of the interface)

<span style="color: #008000">#</span><span style="color: #008000">返回结果类型为字典:{'msg': 'pass'},字典取值操作比较方法</span>
res_dic = requests.<span style="color: #ff0000">post</span>(url, data).json()
The interface called by the post request is implemented as follows:
<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 request - the parameter type is 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 request - the parameter type is json, multi-layer json nesting

<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()
The interface called by post request, json parameters are passed, the code is implemented as follows:
<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 request - upload file

<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)
The upload interface called by post request, the code is implemented as follows:
<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> <filestorage:></filestorage:></class></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 request - add 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()

欢迎评论~~~

 

The above is the detailed content of python notes 8: requests module. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
python中CURL和python requests的相互转换如何实现python中CURL和python requests的相互转换如何实现May 03, 2023 pm 12:49 PM

curl和Pythonrequests都是发送HTTP请求的强大工具。虽然curl是一种命令行工具,可让您直接从终端发送请求,但Python的请求库提供了一种更具编程性的方式来从Python代码中发送请求。将curl转换为Pythonrequestscurl命令的基本语法如下所示:curl[OPTIONS]URL将curl命令转换为Python请求时,我们需要将选项和URL转换为Python代码。这是一个示例curlPOST命令:curl-XPOSThttps://example.com/api

Python爬虫Requests库怎么使用Python爬虫Requests库怎么使用May 16, 2023 am 11:46 AM

1、安装requests库因为学习过程使用的是Python语言,需要提前安装Python,我安装的是Python3.8,可以通过命令python--version查看自己安装的Python版本,建议安装Python3.X以上的版本。安装好Python以后可以直接通过以下命令安装requests库。pipinstallrequestsPs:可以切换到国内的pip源,例如阿里、豆瓣,速度快为了演示功能,我这里使用nginx模拟了一个简单网站。下载好了以后,直接运行根目录下的nginx.exe程序就可

详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

Python如何使用Requests请求网页Python如何使用Requests请求网页Apr 25, 2023 am 09:29 AM

Requests继承了urllib2的所有特性。Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的URL和POST数据自动编码。安装方式利用pip安装$pipinstallrequestsGET请求基本GET请求(headers参数和parmas参数)1.最基本的GET请求可以直接用get方法&#39;response=requests.get("http://www.baidu.com/"

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

python requests post如何使用python requests post如何使用Apr 29, 2023 pm 04:52 PM

python模拟浏览器发送post请求importrequests格式request.postrequest.post(url,data,json,kwargs)#post请求格式request.get(url,params,kwargs)#对比get请求发送post请求传参分为表单(x-www-form-urlencoded)json(application/json)data参数支持字典格式和字符串格式,字典格式用json.dumps()方法把data转换为合法的json格式字符串次方法需要

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool