#!/usr/bin/env python
#_*_coding:utf-8_*_ import re
import time
import json
import MySQLdb
import contextlib hosts = {0:{"host":"192.168.1.140","port":3306,"user":"root","Password":"123456","database":"test"}} def who_binlog():
map_sql = "INSERT INTO `rdstest`.`ty` SET @1=14 @2=5 @3=6 INSERT INTO `rdstest`.`ty` SET @1=12 @2=6 @3=8"
r1 = re.compile("INSERT INTO")
print r1.split(map_sql) for mp in r1.split(map_sql):
if mp:
print(mp.strip())
vals = re.findall('@*.=(\d+)',mp)
print(vals)
vals2 = re.findall("@2=(\d+)",mp)
print(vals2)
db_table = mp.strip().split()[0]
print(db_table) def return_obj():
ret_dirct = {}
ret_dirct["io_sql"] = "test"
ret_dirct["name"] = "oh"
bb = {}
bb["case1"]="test"
bb["case2"] = "test2"
ret_dirct["data"] = bb; print(json.dumps(ret_dirct)) @contextlib.contextmanager
def mysql(Host,Port,User,Password,Database): conn = MySQLdb.connect(host=Host, port=Port, user=User, passwd=Password, db=Database)
#cursor = conn.cursor()
cursor = conn.cursor(MySQLdb.cursors.DictCursor); try:
yield cursor;
finally:
conn.commit();
cursor.close();
conn.close(); def execSql(excelSql):
hosts = {0:{"host":"192.168.1.140","port":3306,"user":"root","Password":"123456","database":"test"}}
for idx in hosts:
ip = hosts[idx]["host"]
port = hosts[idx]["port"]
user = hosts[idx]["user"]
password = hosts[idx]["Password"]
database = hosts[idx]["database"]
with mysql(ip,port,user,password,database) as cursor:
sql = excelSql
cursor.execute(sql)
rows = cursor.fetchall()
if len(rows)>0:
Master_Host = rows[0]["Master_Host"]
SQL_THREAD = rows[0]["Slave_SQL_Running"]
IO_THREAD = rows[0]["Slave_IO_Running"]
LAST_ERROR= rows[0]["Last_Error"]
Slave_SQL_RunStatus= rows[0]["Slave_SQL_Running_State"]
Master_InforStatus = rows[0]["Master_Info_File"]
print("-------DataServer:%s------" % ip)
print("relp Master Host: %s" % Master_Host)
print("repl SQL Thread: %s" % SQL_THREAD)
print("repl IO Thread: %s" % IO_THREAD)
print("Mrepl Last error: %s" % LAST_ERROR)
print("Slave SQLRunStatus: %s" % Slave_SQL_RunStatus)
print("Master_InfoStatus: %s" % Master_InforStatus)
print("\n") def get_table_row(sql):
for idx in hosts:
ip = hosts[idx]["host"]
port = hosts[idx]["port"]
user = hosts[idx]["user"]
password = hosts[idx]["Password"]
database = hosts[idx]["database"]
with mysql(ip, port, user, password, database) as cursor:
cursor.execute(sql)
rows = cursor.fetchall()
return rows if __name__ == "__main__":
#who_binlog()
#return_obj()
#execSql("show slave status;")
db = "test"
tb = "t1"
sql = """select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME COLUMN_KEY,ORDINAL_POSITION
from information_schema.COLUMNS
where TABLE_SCHEMA='%s' and TABLE_NAME='%s'
order by ORDINAL_POSITION asc;
""" %(db,tb)
rows = get_table_row(sql)
print(rows)

python re 实例的更多相关文章

  1. python基础——实例属性和类属性

    python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...

  2. python 发送邮件实例

    留言板回复作者邮件提醒 -----------2016-5-11 15:03:58-- source:python发送邮件实例

  3. python Cmd实例之网络爬虫应用

    python Cmd实例之网络爬虫应用 标签(空格分隔): python Cmd 爬虫 废话少说,直接上代码 # encoding=utf-8 import os import multiproces ...

  4. Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取

    很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...

  5. Python爬虫实例:爬取猫眼电影——破解字体反爬

    字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...

  6. Python爬虫实例:爬取豆瓣Top250

    入门第一个爬虫一般都是爬这个,实在是太简单.用了 requests 和 bs4 库. 1.检查网页元素,提取所需要的信息并保存.这个用 bs4 就可以,前面的文章中已经有详细的用法阐述. 2.找到下一 ...

  7. python 创建实例--待完善

    今天好好琢磨一下 python 创建实例的先后顺序 一. 就定义一个普通类 Util (默认)继承自 object,覆写 new ,init 方法 class Util(object): def __ ...

  8. pcapng文件的python解析实例以及抓包补遗

    为了弥补pcap文件的缺陷,让抓包文件可以容纳更多的信息,pcapng格式应运而生.关于它的介绍详见<PCAP Next Generation Dump File Format> 当前的w ...

  9. 生产消费者模式与python+redis实例运用(中级篇)

    上一篇文章介绍了生产消费者模式与python+redis实例运用(基础篇),但是依旧遗留了一个问题,就是如果消费者消费的速度跟不上生产者,依旧会浪费我们大量的时间去等待,这时候我们就可以考虑使用多进程 ...

  10. 生产消费者模式与python+redis实例运用(基础篇)

    根据这个图,我们举个简单的例子:假如你去某个餐厅吃饭,点了很多菜,厨师要一个一个菜的做,一个厨师不可能同时做出所有你点的菜,于是你有两个选择:第一个,厨师把所有菜都上齐了,你才开始吃:还有一个选择,做 ...

随机推荐

  1. linux c 检测ip变化

    #include <string.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <ne ...

  2. 解决vi编辑器不能使用方向键和退格键

    1.使用vi命令时,不能正常编辑文件,使用方向键时老是出现很多字母 这个问题主要是新系统直装了vi,没有装vim.因为vi是不能直接按退格键删除字符的,所以当你使用退格键删除字符,只有在按下esc时, ...

  3. Python之路,第五篇:Python入门与基础5

    python 循环语句 作用:  根据一定的条件,重复的执行一个或多个语句 两种循环语句: while 语句 for 语句 while 语句: 语法: while    真值表达式: 语句1 ... ...

  4. [LeetCode&Python] Problem 389. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  5. go:基于时间轮定时器方案

    /* * http://blog.csdn.net/yueguanghaidao/article/details/46290539 * 修改内容:为定时器增加类型和参数属性,修改回调函数类型 */ p ...

  6. HDU - 2892:area (圆与多边形交 求面积)

    pro:飞行员去轰炸一个小岛,给出炸弹落地点的位置信息,以及轰炸半径:按顺时针或者逆时针给出小岛的边界点. 求被轰炸的小岛面积. sol:即是求圆和多边形的面积交. (只会套板子的我改头换面,先理解然 ...

  7. 单调栈运用2----(xdoj-1156)

    #include <bits/stdc++.h> using namespace std; ; int num[N]; int len; int top; int t; deque < ...

  8. BinarySearch(Java)

    private int binarySearch(int[] input, int target) { if (input == null) { return -1; } int index1 = 0 ...

  9. Blender 画正四面体

    正四面体打开“添加网格”菜单(Shift + A),然后选择“锥形”.将“顶点数”设置为3,将“半径1”保留为默认值1.000,将“半径2”设置为0.000.现在,将深度设置为 {根号2,约等于1.4 ...

  10. 【二分图最大权完美匹配】【KM算法】【转】

    [文章详解出处]https://www.cnblogs.com/wenruo/p/5264235.html KM算法是用来求二分图最大权完美匹配的.[也就算之前的匈牙利算法求二分最大匹配的变种??] ...