• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            學著站在巨人的肩膀上

            金融數學,InformationSearch,Compiler,OS,

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              12 隨筆 :: 0 文章 :: 8 評論 :: 0 Trackbacks

            前一陣花了點時間學習python,近段時間完成了一個監控服務器基本信息的項目,都是為了滿足大家監控的欲望,特殊日志并報警的分布式系統,單臺服務器采集粒度為1次/1分鐘,一天大約1440條,目前監控了20多臺服務器,一天大約31680條日志,現在單點監控中心服務器在性能上還綽綽有余,有更多的服務器來測試就好了,估計可以支持到100臺以上服務器監控的級別。

            現在遇到一個需求是發現報警時實時發送消息給相關人員,由于公司短信網關只買了上海電信用戶沒有上海電信的號碼,汗一個,只好通過發郵件來實施。

            支持發送GB18030編碼的文本內容,任意編碼附件,可以做出適當修改支持群發。

             

            ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
            1. #coding=utf-8   
            2. #!/usr/lib/python2.5/bin/python   
            3. import os   
            4. import sys   
            5. from smtplib import SMTP   
            6. from email.MIMEMultipart import MIMEMultipart   
            7. from email.mime.application import MIMEApplication   
            8. from email.MIMEText import MIMEText   
            9. from email.MIMEBase import MIMEBase   
            10. from email import Utils,Encoders   
            11. import mimetypes   
            12. import time   
            13.   
            14. STMP_SERVER = "mail.×××.com"  
            15. STMP_PORT = "25"  
            16. USERNAME = "×××@×××.com"  
            17. USERPASSWORD = "×××"  
            18. FROM = "MonitorCenterWarning@×××.com"  
            19. TO = "×××@gmail.com"  
            20.   
            21. def sendFildByMail(config):   
            22.     print 'Preparing...'  
            23.     message = MIMEMultipart( )   
            24.     message['from'] = config['from']   
            25.     message['to'] = config['to']   
            26.     message['Reply-To'] = config['from']   
            27.     message['Subject'] = config['subject']   
            28.     message['Date'] = time.ctime(time.time())   
            29.     message['X-Priority'] =  '3'  
            30.     message['X-MSMail-Priority'] =  'Normal'  
            31.     message['X-Mailer'] =  'Microsoft Outlook Express 6.00.2900.2180'  
            32.     message['X-MimeOLE'] =  'Produced By Microsoft MimeOLE V6.00.2900.2180'  
            33.        
            34.     if 'file' in config:   
            35.         #添加附件   
            36.         f=open(config['file'], 'rb')   
            37.         file = MIMEApplication(f.read())   
            38.         f.close()   
            39.         file.add_header('Content-Disposition''attachment', filename= os.path.basename(config['file']))   
            40.         message.attach(file)   
            41.        
            42.     if 'content' in config:   
            43.         #添加文本內容   
            44.         f=open(config['content'], 'rb')   
            45.         f.seek(0)   
            46.         content = f.read()   
            47.         body = MIMEText(content, 'base64''gb2312')   
            48.         message.attach(body)   
            49.   
            50.     print 'OKay'  
            51.     print 'Logging...'  
            52.     smtp = SMTP(config['server'], config['port'])   
            53.     #如果SMTP服務器發郵件時不需要驗證登錄則對下面這行加上注釋   
            54.     smtp.login(config['username'], config['password'])   
            55.     print 'OK'  
            56.        
            57.     print 'Sending...',   
            58.     smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())   
            59.     print 'OK'  
            60.     smtp.close()   
            61.     time.sleep(1)   
            62.   
            63. if __name__ == "__main__":   
            64.     if len(sys.argv) < 2:   
            65.         print 'Usage: python %s contentfilename' % os.path.basename(sys.argv[0])   
            66.         print 'OR Usage: python %s contentfilename attachfilename' % os.path.basename(sys.argv[0])   
            67.         wait=raw_input("quit.")   
            68.         sys.exit(-1)   
            69.     elif len(sys.argv) == 2:   
            70.         sendFildByMail({   
            71.             'from': FROM,   
            72.             'to': TO,   
            73.             'subject''[MonitorCenter]Send Msg %s' % sys.argv[1],   
            74.             'content': sys.argv[1],   
            75.             'server': STMP_SERVER,   
            76.             'port': STMP_PORT,   
            77.             'username': USERNAME,   
            78.             'password': USERPASSWORD})   
            79.     elif len(sys.argv) == 3:   
            80.         sendFildByMail({   
            81.             'from': FROM,   
            82.             'to': TO,   
            83.             'subject''[MonitorCenter]Send Msg and File %s %s' % (sys.argv[1], sys.argv[2]),   
            84.             'content': sys.argv[1],   
            85.             'file': sys.argv[2],   
            86.             'server': STMP_SERVER,   
            87.             'port': STMP_PORT,   
            88.             'username': USERNAME,   
            89.             'password': USERPASSWORD})   
            90.     wait=raw_input("end.")  

             

            windows xp下:

            例子

             linux ubuntu,suse下:

            1

            收到的結果:

            2

            posted on 2010-03-15 19:24 學者站在巨人的肩膀上 閱讀(671) 評論(0)  編輯 收藏 引用
            亚洲а∨天堂久久精品9966| 99久久精品日本一区二区免费| 国内精品欧美久久精品| 国产成人精品久久| 97精品国产97久久久久久免费| 久久久久久亚洲精品成人| 国产精品久久精品| 久久久综合香蕉尹人综合网| 99久久国产综合精品女同图片| 久久精品国产亚洲麻豆| 亚洲?V乱码久久精品蜜桃| 国产精品美女久久久| 久久青青草视频| 久久久无码精品亚洲日韩软件| 精品熟女少妇a∨免费久久| 久久受www免费人成_看片中文| 久久精品国产半推半就| 伊人久久大香线蕉AV色婷婷色| 国产精品狼人久久久久影院| 久久国产精品77777| 国产亚洲精品久久久久秋霞| 国产精品午夜久久| 久久久青草久久久青草| 99久久精品午夜一区二区| 亚洲欧美日韩中文久久| 久久天天躁夜夜躁狠狠躁2022| 很黄很污的网站久久mimi色| 久久精品男人影院| 97精品久久天干天天天按摩| 久久久噜噜噜www成人网| 久久精品国产99国产精品导航 | 蜜桃麻豆www久久国产精品| 国产精品一区二区久久| 久久精品aⅴ无码中文字字幕不卡| 久久久久人妻一区二区三区 | 国产精品成人久久久| 色偷偷91久久综合噜噜噜噜| 亚洲欧洲久久久精品| 综合久久一区二区三区 | 久久国产成人午夜AV影院| 久久免费精品一区二区|