36 lines
1.6 KiB
Python
36 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import json,sys
|
|
out={'host':'172.18.192.197','port':3307,'conn':None,'dbs':[],'tables':{}}
|
|
try:
|
|
import pymysql
|
|
except Exception as e:
|
|
out['conn']='IMPORT_ERR '+repr(e); print(json.dumps(out)); sys.exit()
|
|
try:
|
|
c=pymysql.connect(host='172.18.192.197',port=3307,user='root',password='root',connect_timeout=10,read_timeout=60)
|
|
out['conn']='OK '+str(c.get_server_info())
|
|
except Exception as e:
|
|
out['conn']='CONN_ERR '+repr(e); print(json.dumps(out)); sys.exit()
|
|
cur=c.cursor()
|
|
try:
|
|
cur.execute('SHOW DATABASES'); out['dbs']=[r[0] for r in cur.fetchall()]
|
|
except Exception as e: out['dbs_err']=repr(e)
|
|
for db,table,key in [('p2p_mysql','t_firebase','p2p'),('ali_iot','t_firebase','ali'),('p2p_mysql','t_p2p_user','p2p_user')]:
|
|
try:
|
|
cur.execute('SHOW COLUMNS FROM `%s`.`%s`'%(db,table)); cols=[r[0] for r in cur.fetchall()]
|
|
cur.execute('SELECT * FROM `%s`.`%s`'%(db,table))
|
|
rows=[]
|
|
for r in cur.fetchall():
|
|
d={}
|
|
for i,name in enumerate(cols):
|
|
v=r[i]
|
|
if isinstance(v,(bytes,bytearray)): v=v.decode('utf8','replace')
|
|
elif v is not None and not isinstance(v,str): v=str(v)
|
|
d[name]=v
|
|
rows.append(d)
|
|
out['tables'][key]={'db':db,'table':table,'cols':cols,'count':len(rows),'rows':rows}
|
|
except Exception as e:
|
|
out['tables'][key]={'db':db,'table':table,'err':repr(e)}
|
|
for t in out['tables'].values():
|
|
if 'rows' in t and t.get('table')=='t_p2p_user' and len(t['rows'])>50:
|
|
t['rows']=t['rows'][:50]
|
|
print(json.dumps(out, ensure_ascii=False))
|