#coding:utf-8import tornado.web
import tornado.options
import tornado.ioloopfromtornado.options import define,options
define('port',default=8000,help='run on the given port',type=int)
mport tornado.web
import tornado.options
import tornado.ioloopfromtornado.options import define,options
define('port',default=8000,help='run on the given port',type=int)classMainHandler(tornado.web.RequestHandler):
defget(self):
self.write('hello world')
application= tornado.web.Application([(r'/',MainHandler),],debug=True)if __name__=='__main__':
tornado.options.parse_command_line()
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()classMainHandler(tornado.web.RequestHandler):
defget(self):
[root@iZ25vn32h9hZ mnt]# vi pythonSql.py
#coding:utf-8import tornado.web
import tornado.ioloop
import tornado.options
import MySQLdbfromdatetime import datetime,timedeltafromtornado.options import define,options
define('port',default=8000,help='run on given port',type=int)
def readTxt():globalcpu,memory,disk,cache
f1= open('../wuxi147_cpu.txt','r+')
f2= open('../wuxi147_memory.txt','r+')
f3= open('../wuxi147_disk.txt','r+')
f4= open('../wuxi147_cache.txt','r+')try:
cpu= int(float(cpu)*100)
memory= '%.2f'%float(x[0])
memory= int(float(memory)*100)
print memory
x=f3.read().splitlines()
disk= '%.2f'%float(x[0])
disk= int(float(disk)*100)
print disk
x=f4.read().splitlines()
cache= '%.2f'%float(x[0])
cache= int(float(cache)*100)
print cachefinally:
f1.close()
f2.close()
f3.close()
f4.close()
def saveInSql():globalcpu,memory,disk,cache
conn= MySQLdb.connect(host='localhost',port=3306,user='root',passwd='niot',db='test')
cur=conn.cursor()
#cur.execute('create table performance(id int not null auto_increment primary key,cpuUsage int,memoryUsage i
nt,physicalUsage int,coreUsage int)')
cur.execute("insert into performance(cpuUsage,memoryUsage,diskUsage,cacheUsage) values('%d','%d','%d','%d')"
%(cpu,memory,disk,cache))
cur.close()
conn.commit()
conn.close()classMainHandler(tornado.web.RequestHandler):globalcpu,memory,disk,cache
defget(self):
readTxt()
saveInSql()
self.render('example.html',title='Tornado get&post',x1=cpu,x2=memory,x3=disk,x4=cache)if __name__=='__main__':
tornado.options.parse_command_line()
application= tornado.web.Application([(r'/',MainHandler),],debug=True)
application.listen(options.port)
tornado.ioloop.IOLoop.instance().start()