Tornado初探

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('\n')
 31             return False
 32         else:
 33            if pstdout:
 34                stdoutstr = pstdout[0].rstrip('\n')
 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('\n')
 60             else:
 61                 if pstdout:
 62                     resultDict = dict([tuple(i.strip('\n').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('\n')
 83             else:
 84                 if pstdout:
 85                     resultDict = dict([tuple(i.strip('\n').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()

 

posted on 2014-08-16 20:01  @Jin  阅读(287)  评论(0编辑  收藏  举报

导航