python实现简单接口

接口实现

import flask,json
server=flask.Flask(__name__)#__name__代表当前的python文件。把当前的python文件当做一个服务启动

@server.route('/index',methods=['get','post'])#第一个参数就是路径,第二个参数支持的请求方式,不写的话默认是get
def index():
    res={'msg':'my first interface','msg_code':0}
    return json.dumps(res,ensure_ascii=False)

server.run(port=7777,debug=True,host='0.0.0.0')

接口访问

import requests
import json

url=" http://127.0.0.1:7777/index"
header={}
r=requests.get(url,headers=header)
print(r.json())

猜你喜欢

转载自www.cnblogs.com/mghhzAnne/p/10863302.html