服务器之家

服务器之家 > 正文

python+flask实现API的方法

时间:2021-04-21 00:53     来源/作者:kd丹妮儿

本文为大家分享了python+flask实现api的具体方法,供大家参考,具体内容如下

flask 框架

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#-*-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/,即可看到一个网页:

python+flask实现API的方法

flask + flask_restful创建一个简单的应用程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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)

请求:

?
1
2
3
4
5
6
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服务

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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内部循环没有结束!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/kanghui_898/article/details/84025523

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部