Tornado初探

摘要:
作者:金日期:20140816http://www.tornadoweb.cn/documentation1、安装#yum yinstallpython setuptools#easy_install Usetuptools#easy_InstallinstallTornado或yum yinstallpython-pippipinstallTornado2。测试导入器

Auth: Jin

Date: 20140816

http://www.tornadoweb.cn/documentation

1、install
#yum -y install python-setuptools
# easy_install -U setuptools
# easy_install install Tornado

或者
yum -y install python-pip
pip install Tornado


2。测试
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")

application = tornado.web.Application([
(r"/", MainHandler),
])

if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

3.测试2

  1 # coding=utf8
  2 import tornado.ioloop
  3 import tornado.web
  4 import subprocess,os,json
  5 
  6 class MainHandler(tornado.web.RequestHandler):
  7     def get(self):
  8         self.write("Hello, world")
  9 
 10 class GetServerHandler(tornado.web.RequestHandler):
 11     def get(self):
 12         global resultDict
 13         resultDict = {}
 14         sys = os.name
 15         if sys == 'nt':
 16             return False
 17         elif sys == 'posix':
 18             self.getHostname()
 19             self.getDiskinfo()
 20             #return resultDict
 21             resultJson = json.dumps(resultDict,sort_keys=True,indent=4)
 22             self.write(resultJson)
 23     
 24     def Command(self,cmd):
 25         cmd = cmd
 26         pstat=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
 27         pstderr=pstat.stderr.readlines()
 28         pstdout=pstat.stdout.readlines()
 29         if pstderr:
 30             errorstr = pstderr[0].rstrip('
')
 31             return False
 32         else:
 33            if pstdout:
 34                stdoutstr = pstdout[0].rstrip('
')
 35                print stdoutstr
 36                return stdoutstr
 37                
 38     def getHostname(self):
 39         cmd = 'hostname'
 40         data = self.Command(cmd)
 41         if data:
 42            resultDict['hostname'] = data
 43          
 44     def getDiskinfo(self):
 45         cmd = 'df -h'
 46         data = self.Command(cmd)
 47         if data:
 48            resultDict['diskinfo'] = data
 49 
 50 class MemHandler(tornado.web.RequestHandler):
 51     def get(self):
 52         cmd = "cat /proc/meminfo |awk -F: '{print $1,$2}'|awk '{print $1,$2}'"
 53         resultDict = {}
 54         try:
 55             pstat = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
 56             pstderr = pstat.stderr.readlines()
 57             pstdout = pstat.stdout.readlines()
 58             if pstderr:
 59                 resultDict['errorcode'] = "command error:%s " % pstderr[0].rstrip('
')
 60             else:
 61                 if pstdout:
 62                     resultDict = dict([tuple(i.strip('
').split()) for i in pstdout])
 63                 else:
 64                     resultDict['errorcode'] = 'command not output'
 65         except Exception,e:
 66             pass
 67             resultDict['errorcode'] = str(e)
 68             resultJson = json.dumps(resultDict,sort_keys=True,indent=4)
 69             self.write(resultJson)
 70         else:
 71             resultJson = json.dumps(resultDict,sort_keys=True,indent=4)
 72             self.write(resultJson)
 73 
 74     def post(self):
 75         cmd = "cat /proc/meminfo |awk -F: '{print $1,$2}'|awk '{print $1,$2}'"
 76         resultDict = {}
 77         try:
 78             pstat = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
 79             pstderr = pstat.stderr.readlines()
 80             pstdout = pstat.stdout.readlines()
 81             if pstderr:
 82                 resultDict['errorcode'] = "command error:%s " % pstderr[0].rstrip('
')
 83             else:
 84                 if pstdout:
 85                     resultDict = dict([tuple(i.strip('
').split()) for i in pstdout])
 86                 else:
 87                     resultDict['errorcode'] = 'command not output'
 88         except Exception,e:
 89             pass
 90             resultDict['errorcode'] = str(e)
 91             resultJson = json.dumps(resultDict,sort_keys=True,indent=4)
 92             self.write(resultJson)
 93         else:
 94             resultJson = json.dumps(resultDict,sort_keys=True,indent=4)
 95             self.write(resultJson)
 96 
 97 
 98 
 99 application = tornado.web.Application([
100     (r"/", MainHandler),
101     (r"/hostinfo", GetServerHandler),
102     (r"/getmeminfo", MemHandler),
103 ])
104 
105 if __name__ == "__main__":
106     application.listen(8888)
107     tornado.ioloop.IOLoop.instance().start()

免责声明:文章转载自《Tornado初探》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇优秀开源项目的svn地址20169205实验一 Java开发环境的熟悉(Linux+IDEA)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何在tornado中以异步的方式调用同步函数

问题 如何在tornado的coroutine中调用同步阻塞的函数 解决方案 使用python内置标准库的concurrent.futures.ThreadPoolExecutor和tornado.concurrent.run_on_executor 解决示例 a.使用concurrent.futures.ThreadPoolExecutor #-*-co...

django面试题必问

1、谈谈你对http协议的认识。 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议。它可以使浏览器更加高效,使网络传输减少。它不仅保证计算机正确快速地传输超文本文档,还确定传输文档中的哪一部分,以及哪部分内容首先显示(如文本先于图形)等。HTTP是一个应用层协议,由请...

Tornado 【简述】

前言 python 旗下,群英荟萃,豪杰并起。单是用于 web 开发的,就有 webpy、web2py、bottle、pyramid、zope2、flask、tornado、django 等等,不一而足。最近几年较为流行的,大概也就是flask、tornado 和 django 了。 关于以上各个 web 开发框架的性能比较,上网一搜,铺天盖地——这不是...

环境配置——tornado项目准备工作

新建tornado项目后,采用Pycharm作为开发工具,采用Xshell链接Ubuntu模拟服务端方便方便测试。项目编码前进行以下几个方面的配置。 1.Ubuntu配置  1.1安装ssh服务 sudo apt-get install openssh-serve 2.Xshell配置  下载安装Xshell软件,并链接虚拟机 3.需安装包 3.1安装vi...

tornado项目

tornado项目之基于领域驱动模型架构设计的京东用户管理后台 本博文将一步步揭秘京东等大型网站的领域驱动模型,致力于让读者完全掌握这种网络架构中的“高富帅”。 一、预备知识:1.接口: python中并没有类似java等其它语言中的接口类型,但是python中有抽象类和抽象方法。如果一个抽象类有抽象方法,那么继承它的子类必须实现抽象类的所有方法,因此,我...

嵌入式实时操作系统VxWorks入门――开发环境构建[转]

VxWorks 操作系统是美国WindRiver公司于1983年设计开发的一种嵌入式实时操作系统(RTOS),它以其良好的可靠性和卓越的实时性被广泛地应用在通 信、军事、航空、航天等高精尖技术及实时性要求极高的领域中,如卫星通讯、军事演习、弹道制导、飞机导航等。在美国的 F-16、FA-18 战斗机、B-2隐形轰炸机和爱国者导弹上,甚至连1997年4月在火...