#!/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. POJ 1065 Wooden Sticks (贪心)

    There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The st ...

  2. 【Python】UI自动化-1

    一.安装selenium和环境配置 1 pip install selenium 2 三个驱动文件放到d:盘根目录 3 安装火狐版本33 4 安装插件:selenium ide\firebug\fir ...

  3. 批注@SuppressWarnings 的作用

    J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. 一点背景:J2SE 5.0 为 Java 语言增加 ...

  4. MongoDb查询

    1.对于时间,在java中不能模糊查询 @Query(value = "{'create_time': {'$gte': ?0},'ent_id':?1}") Page<St ...

  5. socket 映射服务器程序

    server #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket ...

  6. Spring Boot 揭秘与实战(二) 数据存储篇 - MyBatis整合

    文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. MyBatis整合5. 总结 4.1. 方案一 通过 ...

  7. linux下数学函数

    linux 下如果用数学函数比如sin,需要加上“-lm”参数编译,如:gcc test.c -lglut -lGLU -lGL -lm && ./a.out

  8. ELF文件加载与动态链接(二)

    GOT应该保存的是puts函数的绝对虚地址,这里为什么保存的却是puts@plt的第二条指令呢? 原来“解释器”将动态库载入内存后,并没有直接将函数地址更新到GOT表中,而是在函数第一次被调用时,才会 ...

  9. 2017java文本文件操作(读写操作)

    java的读写操作是学java开发的必经之路,下面就来总结下java的读写操作. 从上图可以开出,java的读写操作(输入输出)可以用“流”这个概念来表示,总体而言,java的读写操作又分为两种:字符 ...

  10. 2018.10.25 CCSP马拉松摸铜归来

    24号体测跑50+1000米. 50米抢跑被罚重跑???然后老年人就只能吊着一口仙气跑第二次50米.然后跑1000米,然后再到宿舍收拾行李赶往地铁站,然后再冲到火车站...(卒) 宾馆,三人挤入二人房 ...