国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Python怎么關(guān)閉warning信息提示

發(fā)布時(shí)間:2021-09-27 17:43 來(lái)源:億速云 閱讀:0 作者:chen 欄目: Mysql 歡迎投稿:712375056

本篇內容介紹了“Python怎么關(guān)閉warning信息提示”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!

背景
在使用 python db 執行sql 命令的時(shí)候,如果執行結果含有warning,則會(huì )被寫(xiě)入到 stderr 展示到終端命令行。其實(shí)這些warning提示信息沒(méi)有任何實(shí)際的作用。
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模塊,通過(guò)該模塊控制系統warning 的輸出形式,關(guān)閉或者打開(kāi)。舉個(gè)例子 
1 將  MySQLdb.Warning warnings 傳入exceptions 以便通過(guò) try  catch 獲取exception 進(jìn)而對其進(jìn)行業(yè)務(wù)邏輯處理。
   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)

代碼實(shí)現
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)



免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

日韩欧美一卡2卡3卡4卡无卡免费201| 欧美做受又硬又粗又大视频| 成在人线AV无码免观看麻豆| 曰韩无码二三区中文字幕| 亚洲精品国产综合麻豆久久99| 巨大欧美黑人XXXXBBBB|