python实现第一个web_python 第一个web程序

#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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值