Python Flask+Windows Service製作

#安裝pywin32
pip install pywin32

#安裝服務
> python WinPyServiceExample.py install
Installing service WinPyServiceExample
Service installed

#更新服務
> python WinPyServiceExample.py update
Changing service configuration
Service updated

#查看服務
mmc Services.msc

#停止服務
> net stop PythonCornerExample

#仍舊存在問題,輸入下列命令調試
python WinPyServiceExample.py debug

#常見問題
a. 檢查Python執行路徑是否在PATH變量中。可以在命令行窗口,輸入python來確認。

b. 確認 C:\Program Files\Python36\Lib\site-packages\win32\pywintypes36.dll 存在(注意: “36” 是指python安裝版本)。如果這個文件不存在,從C:\Program Files\Python36\Lib\site-packages\pywin32_system32\pywintypes36.dll 拷貝到上述目錄下。
WinPyServiceExample.py

 

 

""" 
PythonCornerExample.py
"""

import time
import random
from pathlib import Path
from Winservice import Winservice
from flask import Flask, request, json
from gevent.pywsgi import WSGIServer
from HttpApi import app
import Config

class PythonCornerExample(Winservice):
_svc_name_ = "PyHttpService"
_svc_display_name_ = "PythonHttp服務"
_svc_description_ = "PythonHttp服務"

def start(self):
self.isrunning = True

def stop(self):
self.isrunning = False

def main(self):
#app.run(host="127.0.0.1", port=8000)
ip= str(Config.get('http_host'))
port = int(Config.get('http_port'))
http_server = WSGIServer((ip, port), app)
print("Serving HTTP on "+ip+" port "+str(port)+"...")
http_server.serve_forever()


if __name__ == '__main__':
PythonCornerExample.parse_command_line()

HttpApi.py:

from flask import Flask, request, json
from gevent.pywsgi import WSGIServer
'''
auth:***
desc: http api接口
date:20210202
'''
app = Flask(__name__)

#根據圖片url查詢
@app.route('/vin/imgurl')
def vinCodeByImage():
imgUrl = request.args.get("imgurl")
if imgUrl=='' or len(imgUrl) ==0:
return json.dumps({"error":True,"data":'',"message":'imgUrl could not be null'}, ensure_ascii=False, encoding="UTF-8")
zpSaas=ZpSaas()
jsonObj=zpSaas.checkVin(imgUrl)
error=False
mesaage=''
if jsonObj['code']=='':
error=True
mesaage='未識別'
else:
error = False
mesaage = '成功識別'
return json.dumps({"error": error, "data": jsonObj, "message": mesaage}, ensure_ascii=False, encoding="UTF-8")

原文:https://www.jianshu.com/p/13302948dbe6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章