[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)
目的:
1. 通过网页读取watchdog的信息
2. 通过网页设置watchdog
准备工作:
1. 选择一个web框架,选用 cherrypy
$ sudo apt-get install python-cherrypy3
2. 熟悉 RESTFUL , 参考 RESR_API(Mark McLoughlin) redhat REST API 指导
步骤:
我们选择了一个cherrypy作为web框架。 cherrypy的部署简单。
这只是个demo,没有实现MVC,大家自己练习。
此外也没有实现模板,距离一个正式的网站还差的很远。
界面实现如下:
代码如下:
代码中,喂狗代码采用了一个线程。需要改进。
发现不是很好,因为每个wsgi的请求都是一个线程。
最好是采用cherrypy提供的background, 或者将该逻辑独立为一个单例模式。
watchdog.py
1 import cherrypy 2 import json 3 import time 4 import threading 5 6 7 index_html = """ 8 <html> 9 <head> 10 <style type="text/css"> 11 .watchdog-state { 12 display: inline-block; 13 height: 16px; 14 width: 16px; 15 border-radius: 8px; 16 margin-left: 10px; 17 margin-right: 10px; 18 } 19 .up { 20 background: linear-gradient(to bottom, #BFD255 0%%, #8EB92A 50%%, 21 #72AA00 51%%, #9ECB2D 100%%) repeat scroll 0 0 transparent; 22 } 23 24 .down { 25 background: linear-gradient(to bottom, #AFAFAF 0%%, #AFAFAF 50%%, 26 #AFAFAF 51%%, #AFAFAF 100%%) repeat scroll 0 0 transparent; 27 } 28 </style> 29 </head> 30 31 <body> 32 <script type="text/javascript"> 33 function requestJSON(url, suc, done, err) { 34 var xmlhttp; 35 xmlhttp = new XMLHttpRequest(); 36 xmlhttp.open("POST", url, true); 37 xmlhttp.setRequestHeader("Content-type", "application/json"); 38 xmlhttp.setRequestHeader("dataType", "application/json"); 39 xmlhttp.onreadystatechange=function(){ 40 if (xmlhttp.readyState==4 && xmlhttp.status==200) { 41 var responseContext = xmlhttp.responseText; 42 var jsonobj=eval('('+responseContext+')'); 43 suc(jsonobj); 44 } else{ 45 // alert("readyState: " + xmlhttp.readyState + "status" + xmlhttp.status); 46 } 47 } 48 xmlhttp.send(); 49 } 50 51 function operationWatchdog() 52 { 53 // alert("operationWatchdog"); 54 var url = "watchdog/deactivate"; 55 var action = document.getElementById("watchdog-action"); 56 if (action.innerHTML == "Activate"){ 57 url = "watchdog/activate"; 58 } 59 requestJSON(url, function(data){ 60 // alert(data.value); 61 }); 62 if (action.innerHTML == "Activate"){ 63 action.innerHTML = "Deactivate"; 64 } else { 65 action.innerHTML = "Activate"; 66 } 67 var status = document.getElementById("watch-status"); 68 if (status.className.match("up$")){ 69 status.className = "watchdog-state down"; 70 } else{ 71 status.className = "watchdog-state up"; 72 } 73 } 74 function FeedWatchod() 75 { 76 // alert("FeedWatchod"); 77 var url = "watchdog/feedstop"; 78 var feed = document.getElementById("feed-watchdog"); 79 if (feed.innerHTML == "Start"){ 80 url = "watchdog/feedstart"; 81 } 82 requestJSON(url, function(data){ 83 //alert(data.value); 84 }); 85 if (feed.innerHTML == "Start"){ 86 feed.innerHTML = "Stop"; 87 } else { 88 feed.innerHTML = "Start"; 89 } 90 } 91 </script> 92 <div> 93 Status of watchdog: <span id="watch-status" class="watchdog-state %s"> </span> 94 <button id="watchdog-action" type="button" οnclick="operationWatchdog()">%s</button> 95 </div> 96 <div> 97 Feed watchdog: 98 <button id="feed-watchdog" type="button" οnclick="FeedWatchod()">Start</button> 99 </div> 100 </body> 101 </html> """ 102 103 def jsonObj(data): 104 cherrypy.response.headers['Content-Type'] = 'application/json;charset=utf-8' 105 return json.dumps(data, indent=2, separators=(',', ':')) 106 107 watchdog_hadle = open("/dev/cwd_demo", "w+") 108 def watchdogStatus(): 109 vals = watchdog_hadle.readlines(); 110 return vals[0] == ['B\x01\n'] 111 112 def watchdogAction(action): 113 watchdog_hadle.write(action) 114 watchdog_hadle.flush() 115 116 class WelcomePage(object): 117 def __init__(self): 118 self.watchdog = Watchdog() 119 120 def index(self): 121 watchdog_state = "down" 122 watchdog_action = "Activate" 123 if watchdogStatus(): 124 watchdog_state = "up" 125 watchdog_action = "Deactivate" 126 a = index_html % (watchdog_state, watchdog_action) 127 print a 128 return a 129 index.exposed = True 130 131 132 def default(*args, **kwargs): 133 data = {"hello": "world"} 134 return jsonObj(data) 135 default.exposed = True 136 137 138 139 class Watchdog(object): 140 exposed = True 141 feed_running = False 142 feed_thread = None 143 144 def __init__(self): 145 pass 146 147 @classmethod 148 def worker(cls): 149 while cls.feed_running: 150 print "worker" 151 watchdogAction('t') 152 time.sleep(1) 153 return 154 155 @cherrypy.expose 156 def index(self, *args, **kwargs): 157 data = {"value": False} 158 data = {"value": watchdogStatus()} 159 return jsonObj(data) 160 161 @cherrypy.tools.accept(media='application/json') 162 @cherrypy.expose 163 def activate(self, *args, **kwargs): 164 watchdogAction('a') 165 print "activate" 166 raise cherrypy.InternalRedirect("/watchdog") 167 168 @cherrypy.tools.accept(media='application/json') 169 @cherrypy.expose 170 def deactivate(self, *args, **kwargs): 171 watchdogAction('d') 172 print "deactivate" 173 raise cherrypy.InternalRedirect("/watchdog") 174 175 @cherrypy.tools.accept(media='application/json') 176 @cherrypy.expose 177 def feedstart(self, *args, **kwargs): 178 watchdogAction('t') 179 print "feed start" 180 if self.__class__.feed_thread == None: 181 self.__class__.feed_thread = threading.Thread( 182 target=self.__class__.worker) 183 if self.__class__.feed_running == False: 184 self.__class__.feed_running = True 185 self.__class__.feed_thread.start() 186 raise cherrypy.InternalRedirect("/watchdog") 187 188 @cherrypy.expose 189 def feedstop(self, *args, **kwargs): 190 print "feed stop" 191 if self.__class__.feed_thread.isAlive(): 192 self.__class__.feed_running = False 193 self.__class__.feed_thread.join(1.5) 194 self.__class__.feed_thread = None 195 raise cherrypy.InternalRedirect("/watchdog") 196 197 if __name__ == '__main__': 198 # CherryPy always starts with app.root when trying to map request URIs 199 # to objects, so we need to mount a request handler root. A request 200 # to '/' will be mapped to HelloWorld().index(). 201 cherrypy.quickstart(WelcomePage()) 202 else: 203 # This branch is for the test suite; you can ignore it. 204 cherrypy.tree.mount(WelcomePage())
View Code
转载于:https://www.cnblogs.com/shaohef/p/3819382.html
[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)相关推荐
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(三)
我们已经设计了一个基于qemu的watchdog了.下一步工作就是创建一个含有我们的watchdog的虚拟计算机器了. 准备工作: 1. 使用virt-manager或者virsh创建一个虚拟机器. ...
- 解析华为云全栈PaaS全景图,释放云原生计算大未来
在云技术堆栈中,最能体现软件能力之一的就是PaaS平台及服务,在2018中国软件百强企业榜单中,华为已经连续十六年获得第一.华为云基于华为软件能力30年经验的积累,自2016年以来华为云企业应用服务就 ...
- 灵雀云全栈云原生开放平台ACP登陆VMware云市场
近日,国内企业级云原生解决方案的领军企业灵雀云,携全栈云原生开放平台ACP(Alauda Container Platform)正式登陆VMware云市场. VMware云市场(Cloud Marke ...
- 6大新品重磅发布,华为云全栈云原生技术能力持续创新升级
本文分享自华为云社区<HDC.Cloud 2021 | 华为云发布重磅新品,全栈云原生技术能力持续创新升级>,原文作者:技术火炬手 . 4月25日,在华为开发者大会(Cloud)2021上 ...
- Spring Boot+Vue全栈开发实战——花了一个礼拜读懂了这本书
很幸运能够阅读王松老师的<Spring Boot+Vue全栈开发实战>这本书!之前也看过Spring Boot与Vue的相关知识,自己也会使用了Spring Boot+Vue进行开发项目. ...
- Java全栈开发---Java ERP系统开发:商业ERP(八)采购申请
一.采购申请 1.主线业务流程 (1)采购业务流程 采购业务流程图 (2)类图设计 一个订单对应多个订单明细 2.表结构分析 (1)订单与明细 一个订单对应多个订单明细,订单明细是订单内部的一部分 ( ...
- 新IT运维模式下,全栈溯源助你解应用性能监控难题
[51CTO.com原创稿件]2016年,Gartner对APM的定义将原来的五个维度定义修改成了三个维度,即:数字化体验监控(DEM),应用发现.追踪和诊断(ADTD),以及应用分析(AA).此外, ...
- 打造数字原生引擎,易捷行云EasyStack发布新一代全栈信创云
作为新基建的基石,信息技术应用创新产业正迎来黄金发展期.作为企业数字化转型的核心平台, 信创云对下承载包括芯片.整机.操作系统等软硬件基础设施,对上支撑大数据.人工智能.物联网.5G等新一代企业级应用 ...
- 想客户之所想 华为全栈云加速行业云化创新
如今,行业用户从"想上云"到"真正将业务和数据迁移到云上",这中间一直存在一条鸿沟.无论是运营商.金融行业,还是政府.大中型企业,他们都拥有一套比较成熟的IT体 ...
- 天翼云推出全栈政务混合云 支持私有化运行
9月29日,"5G+天翼云+AI 与城市共成长"天翼云全栈政务混合云交流会在安徽合肥开幕,中国电信云计算分公司副总经理李云庄,中国电信安徽公司副总经理郑家升等出席本次大会并参加&q ...
最新文章
- mybatis-mapper
- smarty---设置
- 暂时放在首页,因为需要大家帮忙!!
- mysql存储过程实例_mysql存储过程案例讲解
- 使用LayoutParams设置布局
- 以图搜图 图像匹配_图像匹配,基于深度学习DenseNet实现以图搜图功能
- html语言鼠标悬停特效,CSS3鼠标悬停文字幻影动画特效
- 记最近分析的一个锁屏幕病毒
- 【转】HashSet的用法
- java多态的简单例子_要JAVA的简单例子,继承\多态的,详细讲解运行的每一步
- 新漏洞可导致攻击者劫持Kindle
- WdatePicker的一些用法
- plc简易电子计算机设计,PLC-电子计算器设计.doc
- npm webstorm配置_怎样使用webstorm中配置nodejs环境及npm
- (笔记)涉及到的WinAPI函数
- 知识竞赛中如何按抢答器才能最先抢到
- NOIP2016 酱油记
- 极智AI | 目标检测实现分享二:听说克莱今天复出了?详解 YOLOv2 算法与克莱检测
- c语言标识符的开头字母能不能大写,c语言标识符(c语言标识符的定义)
- IOS 设置icon 出现的bug The app icon set named “AppIcon“ did not have any applicable content.
热门文章
- 晶体管制程极限之后,多层CPU是否可能?
- C# TextBox输入数字 TextBox输入限制 TextBox输入字符 KeyPress
- matlab卷积反投影,卷积反投影法图象重建.pdf
- discuz 官方站 php 开源论坛,Discuz! 官方站-PHP 开源论坛 - Powered by Discuz! | x.discuz.net contacts...
- css渐变颜色php,css的渐变颜色
- android av和hdmi输出切换代码,AV转HDMI转换器有用吗?
- python字符串_Python的字符串和编码
- mysql auto_increment建表_如何在MySQL中已创建的表中插入AUTO_INCREMENT
- Python实现线程安全队列
- 【死磕 Spring】—– IOC 之 bean 的初始化