码迷,mamicode.com
首页 > 编程语言 > 详细

python 分析redis

时间:2014-12-04 15:50:08      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:python redis mysql


#!/usr/bin/env python

#coding=utf-8

import MySQLdb

import redis

import os

import socket,fcntl,struct

#print redis._file_



def get_ip_address(ifname):

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    return socket.inet_ntoa(fcntl.ioctl(

                s.fileno(),

                0x8915, # SIOCGIFADDR

                struct.pack(‘256s‘, ifname[:15])

                )[20:24])


ip = get_ip_address(‘eth0‘)



r = redis.Redis(host=‘192.168.100.89‘,port=6379,db=1)

info = r.info()

wr = open("a.txt",‘w‘)


for key in info:

    wr.write(‘%s  %s\n‘  % (key, info[key]))

wr.close()


file_object = open(‘a.txt‘)

try:

    all_the_text = file_object.read()

finally:

    file_object.close()

print type(all_the_text)

print all_the_text


db = MySQLdb.connect(user=‘root‘, db=‘redisinfo‘, passwd=‘llllll‘, host=‘localhost‘)

cursor = db.cursor()

sql_content = "insert into info_product(ip,name,jifang,redisinfo) values(‘%s‘, ‘%s‘, ‘%s‘, \"%s\")"%(ip,"tes111t","GZ",all_the_text)

print sql_content

cursor.execute(sql_content)

db.commit()

db.close()

cursor.close()



未完待续


本文出自 “运维工程师DBA之路” 博客,请务必保留此出处http://landline.blog.51cto.com/6065685/1586310

python 分析redis

标签:python redis mysql

原文地址:http://landline.blog.51cto.com/6065685/1586310

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!