python request模块传一个文本_python笔记8:requests模块

简介:

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拼接参数:

importrequests

url= ‘http://127.0.0.1:5000/gettest?username=abc‘

#返回结果类型是字符串:{"msg": "pass"}

res =requests.get(url).text

也可以使用.json(),返回结果类型是字典(注:接口请求的返回值类型 与接口的返回定义类型有关)

#返回结果类型是字典:{‘msg‘: ‘pass‘}

res_dic =requests.get(url).json()

get请求- 请求参数类型是key-value形式:

importrequests

url= ‘http://127.0.0.1:5000/gettest‘

#请传参类型是 name=xxx&pwd=123456

data = {"username": "abc"}#返回结果类型是str:{"msg": "pass"}

res = requests.get(url, data).text

也可以使用.json(),返回结果类型是字典 (注:接口请求的返回值类型 与接口的返回定义类型有关)

#返回结果类型是字典:{"msg": "pass"}

res_dic =requests.get(url, data).json()

get请求调用的接口实现如下:

335b83df261c422459d4afc29ba290e5.png

d1b641f023dd079c9e4a800b96607d9d.gif

1 importflask2 from flask importrequest3 from flask importjsonify4

5 server = flask.Flask(__name__)6 @server.route(‘/gettest‘, methods=[‘get‘])7 deftest1():8 name = request.values.get(‘username‘)9 if name == ‘abc‘:10 #接口返回值类型为字典

11 res = {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值