#!/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 ()
Not to be continued
This article is from the "operational Engineer DBA Way" blog, so be sure to keep this source http://landline.blog.51cto.com/6065685/1586310
Python Analytics Redis