创作不易 只因热爱!!
**
热衷分享,一起成长!
这两三天我在默默付出, 朋友,请为我点个赞! 谢谢!
"你的鼓励就是我努力付出的动力"!!
1. 修正上次任务脚本, 循环每5分钟写入远程服务状态.
上期的错误插明显的, 改一调整一下就行(如下图).
每隔一阵时间, 运行一次删除
2. 增加流程, 确保自动更新远程服务参数.
由于远程服务中断要改api地址指向. 手工切换到读取本地资源, 远程服务在线时, 需再从本地api中转然后再访问远程服务资源, 明显太多余.
脚本任务中再写一个api同理放入 app.py. 作用是脚本发送POST请求, api已经封装好系统参数修改的相关处理.
(1) 任务脚本中增加post请求
import urllib
def post_update_config(rtime, mark):
try:
url ='http://127.0.0.1:4444/path/to/updateconfig'
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/xml;charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}
req = urllib.request.Request(url=url, data=f"<request><rtime>{rtime}</rtime><mark>{mark}</mark></request>".encode(), headers=headers, method="POST")
try:
rep = urllib.request.urlopen(req, timeout=9)
print(rep.read().decode("utf-8"))
except Exception as e:
print(e)
except Exception as e:
print(e)
(2) 任务脚本中main 运行如下请求, 在监测到 15 min内的服务状态发生变化时, 发送以上POST至api. 发送api后的15分钟内不在监测服务状态变化. 远程服务状态 在15min后继续被每5分钟的循环任务所监控.
if sec_time == 1 or sec_time>5:
query_jkk = session.query(Jkk).filter(Jkk.rtime>=int(10*time.time())-9444).all()
if query_jkk:
sec_time = 0
list_jkk = [[row.rtime,row.mark] for row in query_jkk]
if len(list_jkk)>=3 and len([r for r in list_jkk if r[1]==1])!= 0 and len([r for r in list_jkk if r[1]==0])!= 0:
query_jkk_max = session.query(Jkk).filter(Jkk.rtime>=int(10*time.time())-4444).first()
if query_jkk_max:
post_update_jkk(query_jkk_max.rtime, query_jkk_max.mark)
sec_time = 1
session.close()
sec_time += 1
3. python 之flask框架 api的继续完善
# app.py 中加入 updateconfig.py的调用
from path.to.updateconfig import UpdateConfig
api.add_resource(UpdateConfig, '/path/to/updateconfig')
python api示例代码updateconfig.py
from flask import request
from flask import url_for, render_template_string
from api.utils import DataQuery
from flask_restful import Resource
from flask import Response
import sqlite3, time
import os, subprocess
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def response_with(xml_content ="<response>luckywith4</response>"):
res = Response(response = xml_content, status=200, mimetype="application/xml")
res.headers["Content-Type"] = "text/xml; charset=utf-8"
return res
class UpdateConfig(Resource):
def post(self, **kwargs):
def canConnect():
fnull = open(os.devnull, 'w')
result = subprocess.call('ping 10.10.10.10 -c4 -w15', shell = True, stdout = fnull, stderr = fnull)
fnull.close()
return result
try:
root = ET.fromstring(request.data)
for item in root.findall(".//rtime"):
ls_rtime = item.text
for item in root.findall(".//mark"):
ls_mark = item.text
except Exception as e:
print(e)
ls_rtime = ""
ls_mark = ""
result = ""
# 指定数据库文件路径
database_path = r'/path/to/file/jkkping.db'
if ls_mark != "" and ls_rtime != "":
thistime = int(10*time.time())
# 验证时间内的状态
if thistime - int(ls_rtime)<444:
sql_sqlite = f"SELECT rtime,mark from jkkping where rtime>={thistime-444} order by rtime desc limit 1"
try:
conn = sqlite3.connect(database_path)
cur = conn.cursor()
cur.execute(sql_sqlite)
row = cur.fetchone()
if row:
bool_up = False
# 切换 为 在线 ,同时再次ping远程资源,验证准确再更新
if ls_mark=='0' and row[1]==0 and canConnect()==0:
bool_up = True
sql_update = "UPDATE table_CONFIG SET config='http://10.10.10.10:9999' where id='luckywith4' AND config='http://111.111.111.111:4444'"
result = '<response>成功切换到外网</response>'
# 切换 为 本地
elif ls_mark=='1' and row[1]==1 and canConnect()==1:
bool_up = True
sql_update = "UPDATE table_CONFIG SET config='http://111.111.111.111:4444' where id='luckywith4' AND config='http://10.10.10.10:9999'"
result = '<response>成功切换到内网</response>'
if bool_up:
# 请根据自己实际情况 变更系统参数
DataQuery().query(sql_update)
cur.close()
except Exception as e:
print(e)
finally:
conn.close()
return response_with(result)
if __name__ == '__main__':
pass
调试成功后,终于可以方便医务人员工作了, 一码就医.医务人员窗口不再人满为患,患者也能快速地扫码就诊, 结算, 取药…
继续默默付出中…
看到这为我点个赞吧!
gameover --本期完结.
end
**你好呀,我是一个医信行业工程师,喜欢学习,喜欢搞机,喜欢各种捣,也会持续分享,如果喜欢我,那就关注我吧!**
往期精彩:
健康码项目笔记, python之flask框架内新增搭建api(一)
健康码项目笔记, python之flask框架内新增搭建api(二)
笔记: 迷你主机Linux平台安装, 采集盒COM连接呼吸机 (一)
笔记: 迷你主机Linux平台安装, 采集盒COM连接呼吸机 (二)
笔记: 迷你主机Linux平台安装, 采集盒COM连接呼吸机 (三)
笔记: 迷你主机Linux平台安装, 采集盒COM连接呼吸机 (四)
作者|医信工程师随笔|Carltiger_github
图片|stable diffusiom|侵删
关注我,我们共同成长
“你的鼓励就是我分享的动力”