検索

Python監視ロガー

Oct 18, 2016 am 09:22 AM

以下の機能を備えたシンプルなログ監視スクリプト: 1. Windows 環境 2. ログキーワードが一致するとサウンドが発せられ、一致したキーワードごとに異なるサウンドが再生されます 3. リアルタイム応答を実現できます

注: はいwin環境

コードをアップロードするだけ

#!/usr/bin/env python
# encoding: utf-8
   
"""
MonitorLog.py
   
Usage: MonitorLog.py ...
Monitor the log file
   
-f  log file
-h  help info
   
python MonitorLog.py -f C:\monitor.log
   
"""
   
import sys
import os
import getopt
import subprocess
import time
import codecs
import winsound
   
ABSPATH = os.path.dirname(os.path.abspath(__file__))
MONITERCONF = 'moniter_keyword.txt' #utf8 file
   
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hf:')
    except getopt.GetoptError, err:
        print str(err)
        print __doc__
        return 1
   
    path = ''
    for k, v in opts:
        if k == '-f':
            path = v
        elif k == '-h':
            print __doc__
            return 0
   
    if not (path and os.path.exists(path)):
        print 'Invalid path: %s' % path 
        print __doc__
        return 2
   
    #命令行元组
    cmd = ('tail', '-f', path)
    print ' '.join(cmd)
    output = subprocess.Popen(cmd, stdout=subprocess.PIPE)
   
    keywordMap = {}
    #加载监控的关键字信息
    with codecs.open(os.path.join(ABSPATH, MONITERCONF), 'r', 'utf8') as f:
        lines = f.readlines()
    for line in lines:
        line = line.strip()
        if not line:
            continue
        keyword, wav = line.strip().split(':')
        keywordMap[keyword] = wav
   
    while True:
        line = output.stdout.readline()
        #process code,得到输出信息后的处理代码
        if not line:
            time.sleep(0.01)
            continue
        line = line.strip().decode('utf8')
        print line
        for keyword in keywordMap:
            if line.find(keyword) > -1:
                winsound.PlaySound(keywordMap[keyword], 
                                   winsound.SND_NODEFAULT)
        #time.sleep(0.01)
    return 0
   
if __name__ == '__main__':
    sys.exit(main())


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Pythonアレイで実行できる一般的な操作は何ですか?Pythonアレイで実行できる一般的な操作は何ですか?Apr 26, 2025 am 12:22 AM

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

一般的に使用されているnumpy配列はどのようなアプリケーションにありますか?一般的に使用されているnumpy配列はどのようなアプリケーションにありますか?Apr 26, 2025 am 12:13 AM

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

Pythonのリスト上の配列を使用するのはいつですか?Pythonのリスト上の配列を使用するのはいつですか?Apr 26, 2025 am 12:12 AM

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

すべてのリスト操作は配列でサポートされていますか?なぜまたはなぜですか?すべてのリスト操作は配列でサポートされていますか?なぜまたはなぜですか?Apr 26, 2025 am 12:05 AM

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト

Pythonリストの要素にどのようにアクセスしますか?Pythonリストの要素にどのようにアクセスしますか?Apr 26, 2025 am 12:03 AM

toaccesselementsinapythonlist、useindexing、negativeindexing、slicing、oriteration.1)indexingstartsat0.2)negativeindexingAcsesess.3)slicingextractStions.4)reterationSuseSuseSuseSuseSeSeS forLoopseCheckLentlentlentlentlentlentlenttodExeror。

Pythonを使用した科学コンピューティングでアレイはどのように使用されていますか?Pythonを使用した科学コンピューティングでアレイはどのように使用されていますか?Apr 25, 2025 am 12:28 AM

Arraysinpython、特にvianumpy、arecrucialinscientificComputing fortheirefficienty andversitility.1)彼らは、fornumericaloperations、data analysis、andmachinelearning.2)numpy'simplementation incensuresfasteroperationsthanpasteroperations.3)arayableminablecickick

同じシステムで異なるPythonバージョンをどのように処理しますか?同じシステムで異なるPythonバージョンをどのように処理しますか?Apr 25, 2025 am 12:24 AM

Pyenv、Venv、およびAnacondaを使用して、さまざまなPythonバージョンを管理できます。 1)Pyenvを使用して、複数のPythonバージョンを管理します。Pyenvをインストールし、グローバルバージョンとローカルバージョンを設定します。 2)VENVを使用して仮想環境を作成して、プロジェクトの依存関係を分離します。 3)Anacondaを使用して、データサイエンスプロジェクトでPythonバージョンを管理します。 4)システムレベルのタスク用にシステムPythonを保持します。これらのツールと戦略を通じて、Pythonのさまざまなバージョンを効果的に管理して、プロジェクトのスムーズな実行を確保できます。

標準のPythonアレイでnumpyアレイを使用することの利点は何ですか?標準のPythonアレイでnumpyアレイを使用することの利点は何ですか?Apr 25, 2025 am 12:21 AM

numpyarrayshaveveraladvantages-averstandardpythonarrays:1)thealmuchfasterduetocベースのインプレンテーション、2)アレモレメモリ効率、特にlargedatasets、および3)それらは、拡散化された、構造化された形成術科療法、

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール