本篇內(nèi)容介紹了“Python怎么關(guān)閉warning信息提示”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)專注于昆山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供昆山營銷型網(wǎng)站建設(shè),昆山網(wǎng)站制作、昆山網(wǎng)頁設(shè)計、昆山網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造昆山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供昆山網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。背景
在使用 python MySQLdb 執(zhí)行sql 命令的時候,如果執(zhí)行結(jié)果含有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模塊,通過該模塊控制系統(tǒng)warning 的輸出形式,關(guān)閉或者打開。舉個例子
1 將 MySQLdb.Warning warnings 傳入exceptions 以便通過 try catch 獲取exception 進而對其進行業(yè)務(wù)邏輯處理。
filterwarnings('error', category=MySQLdb.Warning)
如果設(shè)置為error 則需要在代碼中引入 except機制
try:
execSQL ...
except MySQLdb.Warning, e:
print "MySQLdb Warning", e
2 設(shè)置為warning 為 ignore ,忽略所有warning 信息。
filterwarnings('ignore', category = MySQLdb.Warning)
代碼實現(xiàn)
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怎么關(guān)閉warning信息提示”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)頁標題:Python怎么關(guān)閉warning信息提示-創(chuàng)新互聯(lián)
當前路徑:http://www.rwnh.cn/article28/dsddcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)建站、云服務(wù)器、網(wǎng)站策劃、服務(wù)器托管、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容