java 单机接口限流处理方案
788
2022-06-06
本文为大家分享了python+flask实现API的具体方法,供大家参考,具体内容如下
Flask 框架
#-*-coding:utf-8-*- #pip install flask #pip install flask-restful from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "hello world!" if __name__ == '__main__': app.run(debug=True)
pycharm运行该程序后,在浏览器输入http://127.0.0.1:5000/,即可看到一个网页:
Flask + flask_restful创建一个简单的应用程序:
from flask import Flask from flask_restful import Resource,Api app = Flask(__name__) api = Api(app) class HelloWorld(Resource): def get(self): return {"hello":"world"} api.add_resource(HelloWorld,'/') if __name__ == '__main__': app.run(debug=True)
python+flask创建API: 获取post请求传递的json数据
from flask import Flask,abort,jsonify,make_response,request app = Flask(__name__) @app.route('/analyze/' ,methods=['POST']) def call_wiscan_analyze(): if not request.json or not 'path' in request.json: abort(400) path = request.json['path'] if __name__ == '__main__': app.run(port='50055',debug=True)
请求:
from requests import post if __name__ == '__main__': path='"F:/nb_org_data/86574FG01/2013/1029/0008/86574FG01201310290008.img"' ret = post('http://localhost:50055/analyze/',json={'path':path}) print(ret.text)
将API封装为win32服务
import win32serviceutil import win32service import win32event import win32timezone from flask import Flask,abort,jsonify,make_response,request import threading app = Flask(__name__) _ONE_DAY_IN_SECONDS = 60 * 60 * 24 @app.route('/analyze/' ,methods=['POST']) def call_wiscan_analyze(): if not request.json or not 'path' in request.json: abort(400) path = request.json['path'] def thread_target(): app.run(port='50055', debug=True) class GRPCWin32Client(win32serviceutil.ServiceFramework): _svc_name_ = 'GRPCWin32Client' _svc_display_name_ = 'Nuctech GRPC Client' _svc_description_ = 'wiscan grpc client' def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) #self.logger = self._getLogger() self.run = True def SvcDoRun(self): th = threading.Thread(target=thread_target) th.start() try: while self.run: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: pass pass def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) self.run = False if __name__ == '__main__': import sys import servicemanager if len(sys.argv) == 1: try: evtsrc_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(GRPCWin32Client) servicemanager.Initialize('GRPCWin32Client', evtsrc_dll) servicemanager.StartServiceCtrlDispatcher() except win32service.error as details: import winerror if details == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage() else: win32serviceutil.HandleCommandLine(GRPCWin32Client)
注意:
启动一个线程运行app.run(),否则安装完win32服务,启动服务后,无法停止服务,因为app内部循环没有结束!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~