实现Python连接Mysqln以及应用
# cat mem/mysql.py# -*- coding:utf-8 -*-
import time
import os
import MySQLdb as mysql
db = mysql.connect(user="root", passwd="123456", db="memory", host="localhost")
db.autocommit(True)
cur=db.cursor()
def saveMem(): ##获取memory的total、free、buff的量
a="awk 'NR==1{print $2}' /proc/meminfo"
file = os.popen(a)
total=int(file.read())
b="awk 'NR==2{print $2}' /proc/meminfo"
file =os.popen(b)
free =int(file.read())
c="awk 'NR==3{print $2}' /proc/meminfo"
file = os.popen(c)
buffer =int(file.read())
d="awk 'NR==4{print $2}' /proc/meminfo"
file =os.popen(d)
cache =int(file.read())
mem_used=total-free-buffer-cache
print mem_used/1024
#mem = mem_used/1024
cur_time = int(time.time())
sql = 'insert into memory (memory, time) value (%s,%s)'%(mem_used,cur_time)
cur.execute(sql)
while True:
saveMem()
time.sleep(1) # sleep 1 second
页:
[1]