利用python写api接口实战指南

作者:说笑谈古松 时间:2021-03-29 06:07:18 

一、操作步骤

  1. 导入:import flask,json

  2. 实例化:api = flask.Flask(name)

  3. 定义接口访问路径及访问方式:@api.route(’/index’,methods=[‘get/post/PUT/DELETE’])

  4. 定义函数,注意需与路径的名称一致,设置返回类型并支持中文:def index(): return json.dumps(ren,ensure_ascii=False)

  5. 三种格式入参访问接口:
    5.1 url格式入参:flask.request.args.get(‘id’)
    5.2 form-data格式入参:pwd = flask.request.values.get(‘pwd’)
    5.3 josn格式入参:pwd = flask.request.json.get(‘pwd’)

  6. 启动服务:api.run(port=8888,debug=True,host=‘127.0.0.1’),开启服务之后,就可以通过ip+端口+路径+入参访问接口

二、源码举例

#!/usr/bin/python3
# encoding:utf-8
import flask,json
# 实例化api,把当前这个python文件当作一个服务,__name__代表当前这个python文件
api = flask.Flask(__name__)

# 'index'是接口路径,methods不写,默认get请求    
@api.route('/index',methods=['get'])
# get方式访问
def index():
 ren = {'msg':'成功访问首页','msg_code':200}
 #json.dumps 序列化时对中文默认使用的ascii编码.想输出中文需要指定ensure_ascii=False
 return json.dumps(ren,ensure_ascii=False)

#post入参访问方式一:url格式参数
@api.route('/article',methods=['post'])
def article():
 #url格式参数?id=12589&name='lishi'
 id = flask.request.args.get('id')

if id:
   if id == '12589':
     ren = {'msg':'成功访问文章','msg_code':200}
   else:
     ren = {'msg':'找不到文章','msg_code':400}
 else:
   ren = {'msg':'请输入文章id参数','msg_code':-1}
 return json.dumps(ren,ensure_ascii=False)

#post入参访问方式二:from-data(k-v)格式参数
@api.route('/login',methods=['post'])
def login():
 #from-data格式参数
 usrname = flask.request.values.get('usrname')
 pwd = flask.request.values.get('pwd')

if usrname and pwd:
   if usrname =='test' and pwd =='123456':
     ren = {'msg':'登录成功','msg_code':200}
   else:
     ren = {'msg':'用户名或密码错误','msg_code':-1}
 else:
   ren = {'msg':'用户名或密码为空','msg_code':1001}
 return json.dumps(ren,ensure_ascii=False)

#post入参访问方式二:josn格式参数  
@api.route('/loginjosn',methods=['post'])
def loginjosn():
 #from-data格式参数
 usrname = flask.request.json.get('usrname')
 pwd = flask.request.json.get('pwd')

if usrname and pwd:
   if usrname =='test' and pwd =='123456':
     ren = {'msg':'登录成功','msg_code':200}
   else:
     ren = {'msg':'用户名或密码错误','msg_code':-1}
 else:
   ren = {'msg':'用户名或密码为空','msg_code':1001}
 return json.dumps(ren,ensure_ascii=False)

if __name__ == '__main__':
 api.run(port=8888,debug=True,host='127.0.0.1') # 启动服务
 # debug=True,改了代码后,不用重启,它会自动重启
 # 'host='127.0.0.1'别IP访问地址

运行结果:

 * Serving Flask app 'monitor' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 991-833-116
 * Running on http://127.0.0.1:8888/ (Press CTRL+C to quit)
127.0.0.1 - - [16/Jan/2022 14:05:53] "POST /login?usrname=test&pwd=123456 HTTP/1.1" 200 -
127.0.0.1 - - [16/Jan/2022 14:08:34] "GET /index HTTP/1.1" 200 -

请求方式:

使用postman测试接口是否可行

如:

url:127.0.0.1:8888/login

参数:usrname=test;pwd=123456

利用python写api接口实战指南

获取请求参数的几种方法:

flask.request.form.get("key", type=str, default=None) 获取表单数据,
flask.request.args.get("key") 获取get请求参数,
flask.request.values.get("key") 获取所有参数。

来源:https://blog.csdn.net/qq_40468470/article/details/122522758

标签:python,api,接口
0
投稿

猜你喜欢

  • Python中range、np.arange和np.linspace的区别

    2023-10-17 14:45:14
  • 微信小程序简单的canvas裁剪图片功能详解

    2023-08-24 07:49:20
  • 批量获取及验证HTTP代理的Python脚本

    2023-11-19 12:10:34
  • 利用ASP发送和接收XML数据的处理方法

    2009-02-02 08:57:00
  • 正确认识MySQL对服务器端光标的限制

    2008-12-03 15:52:00
  • Python入门教程(八)PythonCasting用法

    2021-11-14 02:20:41
  • Golang CSP并发机制及使用模型

    2023-09-15 16:13:27
  • Python实现批量生成,重命名和删除word文件

    2022-12-03 05:51:33
  • Symfony控制层深入详解

    2023-11-14 20:43:33
  • python使用pil生成缩略图的方法

    2022-06-07 13:07:44
  • apache+mysql+php+ssl服务器之完全安装攻略

    2023-11-16 07:34:16
  • sql server 锁表语句分享

    2012-02-12 15:49:20
  • SQL Server 2005中利用xml拆分字符串序列

    2009-01-06 11:30:00
  • 使用python刷访问量的示例代码

    2023-11-09 12:55:30
  • python自然语言处理之字典树知识总结

    2023-12-29 01:01:21
  • Python实现的ini文件操作类分享

    2022-01-01 12:46:24
  • 使用python3+xlrd解析Excel的实例

    2021-05-09 23:37:33
  • Python基于正则表达式实现计算器功能

    2021-08-17 13:00:02
  • 用ASP实现IE地址栏参数的判断

    2008-10-10 15:54:00
  • Oracle开发之报表函数

    2023-07-23 16:29:00
  • asp之家 网络编程 m.aspxhome.com