python 分析redis


#!/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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。