python 如何调用远程接口
2023-06-13 09:15:43 时间
在python中我们可以使用requests模块来实现调用远程接口
一、安装requests模块pip install requests二、使用requests模块实现get方式调用远程接口
使用get方式调用远程接口主要使用了requests模块的get方法
requests.get()
get方法常见的参数有url,params和headers
url:表示远程接口的地址使用requests模块实现get方式调用远程接口的简单实现如下
# -*- coding: utf-8 -*- import requests import ast #接口地址 url = XXX #get传参 data = {type:0} #headers信息 headers = { Content-Type: application/x-www-form-urlencoded, Authorization: Bearer XXX r = requests.get(url, params=data, headers = headers) # 接口返回的状态码 print(r.status_code) # 接口返回的字符串内容 content = r.text # #将字符串转字典型 content_list = ast.literal_eval(content) print(content_list) # 接口返回的json格式内容 print(r.json())
根据如上就可以实现使用get方式调用远程接口
三、使用requests模块实现post方式调用远程接口使用post方式调用远程接口主要使用了requests模块的post方法
requests.post()
post方法常见的参数有url,data和headers
url:表示远程接口的地址 data:表示post参数 headers:表示post传参的headers参数信息使用requests模块实现post方式调用远程接口的简单实现如下
# -*- coding: utf-8 -*- import requests import ast #接口地址 url = XXX #header信息 headers = { Content-Type: application/x-www-form-urlencoded, Authorization: Bearer XXX #post传参 data = { nickname: 111, gender: 1, city: ce, avatar: 111 r = requests.post(url, data=data,headers=headers) # 接口返回的状态码 print(r.status_code) # 接口返回的字符串内容 content = r.text # #将字符串转字典型 content_list = ast.literal_eval(content) print(content_list) # 接口返回的json格式内容 print(r.json())
本文链接:http://www.yunweipai.com/39355.html
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/53309.html
python相关文章
- python求逆矩阵的方法,Python 如何求矩阵的逆「建议收藏」
- 【记录】mac使用PyCharm中Python版本不对应的解决方法
- python win32api messagebox_如何在Python中使用Win32 API?
- python数组拼接字符串_Python练习题——数组拼接
- python截图识别文字_Python文字截图识别OCR工具实例解析
- python之turtle海龟绘图篇[通俗易懂]
- 【说站】python zip函数的使用注意
- 【说站】python中requests如何优化接口调用
- python使用opencv如何保存图片_OpenCV Python 保存图片
- eval在python中是什么意思_如何在Python中使用eval ?
- 人生苦短,我用Python-手把手教你如何使用python写串口调试助手
- 搭建python开发环境-c++教程 如何搭建Python开发环境?
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(一)
- Python_Day33_网络编程Socket模块1详解编程语言
- Linux 启动 Python编程之旅(linux打开python)
- Python实现快速连接Redis数据库(python连接redis)
- 下的应用玩转Python:Linux下的Python应用之旅(python在linux)
- 用Python仿写MSSQL 编程体验更有趣(python仿mssql)
- Linux中如何离开Python环境(linux怎么退出python)