如何在Python中設定郵件發送日誌?
前幾有人在群組裡問,說了一些關於Python設定郵件發送日誌,我感覺挺漲知識的,所以在這裡就記錄一下以便日後自己使用避坑。
- 我們可以使用Python的logging.handlers.SMTPHandler方法,把日誌傳送到指定的郵件信箱。之前使用 163 郵箱設定參數之後可以放心使用,但是使用QQ的企業郵箱卻一直報錯,總是說登入逾時。
最後透過閱讀logging的原始碼找到了原因,logging預設支援TLS連接,QQ企業信箱和Gmail都是使用SSL連接的。
- 最後附上一個簡單的設定
# logging.conf完整配置 [loggers] keys=root,test [handlers] keys=consoleHandler,fileHandler,testHandler [formatters] keys=simpleFormatter [formatter_simpleFormatter] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s] datefmt= [logger_root] level=INFO handlers=consoleHandler,fileHandler [logger_test] level=INFO handlers=testHandler qualname=test propagate=0 [handler_consoleHandler] class=StreamHandler level=INFO formatter=simpleFormatter args=(sys.stdout,) [handler_fileHandler] class=FileHandler level=INFO formatter=simpleFormatter args=('log/spider_db.log', 'a') [handler_testHandler] class=handlers.SMTPHandler level=INFO formatter=simpleFormatter args=(('smtp.163.com',25), 'username@163.com', ['somebody01@example.com','somebody02@example.com'], 'Test SMTPHandler', ('username', 'password')) python # 邮件测试例子 import logging import logging.config logging.config.fileConfig("logging.conf") logger = logging.getLogger('test') logger.info('hello body ~')
以上是如何在Python中設定郵件發送日誌?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

創建Python虛擬環境可使用venv模塊,步驟為:1.進入項目目錄執行python-mvenvenv創建環境;2.Mac/Linux用sourceenv/bin/activate、Windows用env\Scripts\activate激活;3.使用pipinstall安裝包、pipfreeze>requirements.txt導出依賴;4.注意避免將虛擬環境提交到Git,並確認安裝時處於正確環境。虛擬環境能隔離項目依賴防止衝突,尤其適合多項目開發,編輯器如PyCharm或VSCode也

forNewgo1.21項目,使用logforofficial loggingsupport; 2. forhigh-performanceProductionservices,selectzaporzerologduetototheirspeedandlowallowallowallowallocations; 3.ForeaseofusofusofuseanDrichEandrichIntRichIntrationsLikEsentryHooksEntryHooksEntryHooksEntryHooksEntryHooksEntryhooksEnderGrusIsIdeAdeSiteSiteSiteSitePitElowerPertermesterpersemperance; 4

Usetracemalloctotrackmemoryallocationsandidentifyhigh-memorylines;2.Monitorobjectcountswithgcandobjgraphtodetectgrowingobjecttypes;3.Inspectreferencecyclesandlong-livedreferencesusingobjgraph.show_backrefsandcheckforuncollectedcycles;4.Usememory_prof

use usezoneInInfoforpython3.9 toCreateTimeZone-wardEteTimesandConvertBetBetWeenTimeZonesWithAstimeZone(); 2.Forpython3.6–3.6-3.6-3.6,useppypytzwithlocalize()

使用FlaskBlueprint可以將應用按功能模塊化;1.創建藍圖實例並定義路由,如user.py中創建user_bp;2.在另一文件如post.py中創建其他藍圖;3.在app.py中導入並用app.register_blueprint()註冊各藍圖;4.運行後訪問對應URL即可看到模塊化後的路由效果,代碼結構更清晰且易於維護。

使用Python自動化將Excel數據填入網頁表單的方法是:先用pandas讀取Excel數據,再用Selenium控制瀏覽器自動填寫並提交表單;具體步驟包括安裝pandas、openpyxl和Selenium庫,下載對應瀏覽器驅動,用pandas讀取data.xlsx文件中的Name、Email、Phone等字段,通過Selenium啟動瀏覽器打開目標網頁,定位表單元素並逐行填入數據,使用WebDriverWait處理動態加載內容,添加異常處理和延遲確保穩定性,最後提交表單並循環處理所有數據行

要排序字典的值,使用sorted()函數配合dict.items()和key參數;1.使用lambdaitem:item[1]按值升序排序;2.添加reverse=True實現降序排序;3.可用operator.itemgetter(1)替代lambda以提升可讀性和性能;字典在Python3.7 中保持插入順序,原字典不變,返回新字典,若值類型混雜需額外處理,最終模式為dict(sorted(d.items(),key=lambdax:x[1]))。

要美化打印JSON文件,需使用json模塊的indent參數,具體步驟為:1.使用json.load()讀取JSON文件數據;2.使用json.dump()並將indent設為4或2寫入新文件,即可生成格式化後的JSON文件,完成美化打印。
