Flask Restful服务简单实现

  官网:http://flask.pocoo.org/docs/1.0/quickstart/#routing

  1、安装

    windows下:pip3 install Flask

  具体参照:windows10下部署IIS10+Django web服务

  2、简单代码:

  

from flask import Flask,jsonify,make_response,request,make_response,abort
import weData
import os
# Flask初始化参数尽量使用你的包名,这个初始化方式是官方推荐的,官方解释:http://flask.pocoo.org/docs/0.12/api/#flask.Flask
app = Flask(__name__) @app.route('/getBiz', methods=['GET','POST'])
def get_tasks():
logPath = os.path.join(os.getcwd(), "log")
obj = weData.weData(logPath)
dict1 ={}
# postman调用方式
keyword = request.args.get('keyword')
if not keyword:
# 程序调用方式
keyword = request.json['keyword']
dict1['biz']=obj.catchBiz(keyword)
print(dict1)
return jsonify(dict1) @app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404) if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000,debug=False)

python调用方式

from urllib import request
import requests
import json
#
# import ssl
# ssl._create_default_https_context = ssl._create_unverified_context class test:
def __init__(self):
pass def getBiz(self,keyword):
data_json = {"keyword": keyword}
headers = {'Content-type': 'application/json'}
url = 'http://11.21.41.21:8000/getBiz'
# post调用方式
response = requests.post(url, json=data_json, headers=headers)
# get调用方式
# response = requests.get(url, json=data_json, headers=headers)
dict1 = json.loads(response.text)
biz = dict1.get("biz")
print(keyword, ":", biz) if __name__=="__main__": obj = test()
obj.getBiz("支付宝")

  和django 不同的是设置环境变量:

  只需要设置两个:

  WSGI_HANDLER:WeixinService.app

  PYTHONPATH:E:\WeixinService

  Flask Restful服务简单实现

    Flask Restful服务简单实现

上一篇:Java经典编程题50道之三十八


下一篇:L - Ray in the tube Gym - 101911L (暴力)