网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python怎么关闭warning信息提示-创新互联

本篇内容介绍了“Python怎么关闭warning信息提示”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联专注于昆山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供昆山营销型网站建设,昆山网站制作、昆山网页设计、昆山网站官网定制、微信小程序开发服务,打造昆山网络公司原创品牌,更为您提供昆山网站排名全网营销落地服务。

背景
在使用 python MySQLdb 执行sql 命令的时候,如果执行结果含有warning,则会被写入到 stderr 展示到终端命令行。其实这些warning提示信息没有任何实际的作用。
root@rac3:~/scripts# >python set_ms.py -m 10.0.2.15:3306 -s 10.0.2.6:3308 
Begin to set Replicate on slave 10.0.2.6:3308
set_ms.py:35: Warning: Slave already has been stopped
  ret=cursor.execute(SQL)
set_ms.py:35: Warning: Sending passwords in plain text without SSL/TLS is extremely insecure.
  ret=cursor.execute(SQL)
set_ms.py:35: Warning: Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
  ret=cursor.execute(SQL)
Success : CHANGE MASTER TO MASTER_HOST='10.0.2.15',MASTER_USER='slave',MASTER_PASSWORD='xxxx',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=314;
那我们该怎么解决呢?
Python 提供了warnings模块,通过该模块控制系统warning 的输出形式,关闭或者打开。举个例子 
1 将  MySQLdb.Warning warnings 传入exceptions 以便通过 try  catch 获取exception 进而对其进行业务逻辑处理。
   filterwarnings('error', category=MySQLdb.Warning) 
 如果设置为error 则需要在代码中引入 except机制
  try:
        execSQL ...
   except MySQLdb.Warning, e:
        print "MySQLdb Warning", e

 2 设置为warning 为 ignore ,忽略所有warning 信息。
  filterwarnings('ignore', category = MySQLdb.Warning)

代码实现
import MySQLdb
import MySQLdb.cursors
from warnings import filterwarnings
filterwarnings('ignore', category = MySQLdb.Warning)
def execSql(SQL,IP,PORT,USER,PWD):
  '''####exec_sql####'''
  try:
      db=MySQLdb.connect(host=IP,user=USER,passwd=PWD,db='test',port=int(PORT),charset="utf8")
      cursor=db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
  except MySQLdb.Error,e:
      print "connection failed!Error %d:%s"%(e.args[0],e.args[1])
      sys.exit(9)

Python怎么关闭warning信息提示

“Python怎么关闭warning信息提示”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!


当前标题:Python怎么关闭warning信息提示-创新互联
分享链接:http://bjjierui.cn/article/dsddcp.html

其他资讯