python 第一个web程序

yum install python-setuptools
easy_install pip

 

pip install tornado
yum install MySQL-python

 

test.py里面的内容

#coding:utf-8
import tornado.web
import tornado.options
import tornado.ioloop

from tornado.options import define,options
define('port',default=8000,help='run on the given port',type=int)

class MainHandler(tornado.web.RequestHandler):
        def get(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()
#coding:utf-8
import tornado.web
import tornado.options
import tornado.ioloop

from tornado.options import define,options
define('port',default=8000,help='run on the given port',type=int)

mport tornado.web
import tornado.options
import tornado.ioloop

from tornado.options import define,options
define('port',default=8000,help='run on the given port',type=int)

class MainHandler(tornado.web.RequestHandler):
        def get(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()class MainHandler(tornado.web.RequestHandler):
        def get(self):
[root@iZ25vn32h9hZ mnt]# vi pythonSql.py 
#coding:utf-8
import tornado.web
import tornado.ioloop
import tornado.options
import MySQLdb

from datetime import datetime,timedelta
from tornado.options import define,options
define('port',default=8000,help='run on given port',type=int)

def readTxt():
        global cpu,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 cache
        finally:
                f1.close()
                f2.close()
                f3.close()
                f4.close()

def saveInSql():
        global cpu,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()

class MainHandler(tornado.web.RequestHandler):
        global cpu,memory,disk,cache
        def get(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()

 

posted @ 2015-05-27 15:20  kin2321  阅读(328)  评论(0编辑  收藏  举报