python创建接口有2种方法(目前我知道的)
方法1:
格式:
from flask import Flask#导入库文件
app=Flask(__name_)#定义框架
@app.route("/login",methods=["get","post"])#为接口定义好路由和访问方式
def login():#定义具体接口
xxx
pass
if __name__=="__main__":#运行框架
server.run(host="127.0.0.1",port=8080,debug=True)
完整举例:
from flask import Flask
from flask_restful import request
server=Flask(__name__)
@server.route("/login",methods=["post","get"])
def login():
user_name=request.values.get("name")
user_pwd=request.values.get("pwd")
if user_name and user_pwd :
if user_name=="xiaoming" and user_pwd=="111":
result={"code":200,"message":"登录成功!"}
elif user_name=="xiaoming" and user_pwd !="111":
result={"code":401,"message":"用户名或密码错误!"}
elif user_name != "xiaoming" and user_pwd == "111":
result = {"code": 402, "message": "用户名或密码错误!"}
elif user_name != "xiaoming" and user_pwd != "111":
result = {"code": 403, "message": "用户名或密码错误!"}
else:
result = {"code": 404, "message": "用户名或密码不能为空!"}
return result
if __name__=="__main__":
server.run(host="127.0.0.1",port=8080,debug=True)
方法2:
格式:
from flask import Flask#导入库文件
from flask_restful import Api,Resource,request
app=Flask(__name_)#定义Flask框架
api=Api(app)#定义Api框架
class Login(Resource)#定义接口资源
def get(self):#定义get方式请求该接口
pass
def post(self):#定义post方式请求该接口
pass
api.add_resource(Login,"/login")#给接口加载资源,定义路由
if __name__=="__main__":#运行框架
server.run(host="127.0.0.1",port=8080,debug=True)
完整举例:
from flask import Flask
from flask_restful import Api,Resource,abort,request
POSTS = [
{},
{'title': 'first post', 'content': 'first post'},
{'title': 'last post', 'content': 'last post'},
{'title': 'how to learn interface test', 'content': 'how to learn interface test'}
]
#定义一个判断Post[post_id]是否在列表内
def abort_if_postid_doesnot_exit(post_id):
post_id=int(post_id)
try:
POSTS[post_id]
except IndexError:
abort(401,message="post_id超出范围!")
app=Flask(__name__)#定义一个Flask框架,
api=Api(app)#定义个Api框架
#构造资源Post,一个资源就是一个class
class Post(Resource):
def get(self,post_id):#定义一个get方法,/posts/1
abort_if_postid_doesnot_exit(post_id)#判断是否超出范围
post_id=int(post_id)#确保输入的post_id是int
return POSTS[post_id]
def put(self,post_id):#定义一个put方法,/posts/1
abort_if_postid_doesnot_exit(post_id)#判断是否超出范围
post_id = int(post_id)#确保输入的post_id是int
json_data=request.get_json()#捕获发送的数据,把dict数据以json格式显示
post={"title":json_data["title"],"content":json_data["content"]}
POSTS[post_id]=post
return POSTS[post_id]
def delete(self,post_id):#定义一个delete方法,/posts/1
abort_if_postid_doesnot_exit(post_id)
post_id = int(post_id) # 确保输入的post_id是int
del POSTS[post_id]
return "delete success!"
#构造资源PostList,一个资源就是一个class
class PostList(Resource):
def get(self):#定义一个get方法,/posts
posts=[]
for post in POSTS:
if post :
new_post = {}
new_post["url"]="/posts/"+str(POSTS.index(post))
new_post["title"]=post["title"]
posts.append(new_post)
return posts
def post(self):
post_id = len(POSTS)
json_data=request.get_json(force=True)#获取发送的body.将dict数据已json格式显示
post={"title":json_data["title"],"content":json_data["content"]}
POSTS.append(post)
return POSTS[post_id]
#将资源挂载到api,并规定访问路由
api.add_resource(Post,"/posts/<post_id>")
api.add_resource(PostList,"/posts")
if __name__=="__main__":
app.run(debug=True)
有不明白的可以微我:CPS1016347441