#!/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
原文地址:http://landline.blog.51cto.com/6065685/1586310