python中模拟接口开发

Posted skyxiuli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中模拟接口开发相关的知识,希望对你有一定的参考价值。

# 模拟没有开发好的接口
# pip install flask
import flask
import json

server = flask.Flask(__name__)


@server.route(‘/login‘) # 浏览器访问的名称
def welcome():
data = {‘code‘: 0, ‘msg‘: ‘登录 成功‘, ‘session_id‘: ‘123456‘}
return json.dumps(data, ensure_ascii=False)


@server.route(‘/reg‘)
def reg():
data = {‘code‘: 1, ‘msg‘: ‘test注册成功测试debug‘, ‘session_id‘: ‘123456‘}
return json.dumps(data, ensure_ascii=False)


@server.route(‘/‘)
def index():
str = ‘<h1><a hef="http://www.baidu.com">点我</a></h1>‘
return str

# http://127.0.0.1:6666/
# http://192.168.5.104:6666/login
# 加上host 其他电脑可以通过ip地址访问
server.run(host=‘0.0.0.0‘, port=6666, debug=True) # 默认不写port端口是5000 ,debug=True 自动重启,以后修改不要手动重启了

以上是关于python中模拟接口开发的主要内容,如果未能解决你的问题,请参考以下文章

python:接口开发flask模块

Python-接口开发入门

Python-接口开发入门

python小记13:mock接口开发1

Python学习笔记十五_开发接口

python_接口开发