• <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)  編輯 收藏 引用
            国产精品美女久久久| 亚洲国产欧美国产综合久久| 99久久精品免费看国产一区二区三区| 久久综合九色综合久99| 亚洲国产高清精品线久久| 久久久久青草线蕉综合超碰| av国内精品久久久久影院 | 久久精品国产色蜜蜜麻豆| 久久婷婷五月综合97色直播 | 一本久久a久久精品综合夜夜| 久久国产成人精品国产成人亚洲| 囯产精品久久久久久久久蜜桃| 久久精品人成免费| 亚洲国产成人久久精品99| 精品久久久久久亚洲| 青青草原精品99久久精品66| 久久AⅤ人妻少妇嫩草影院| 无码人妻精品一区二区三区久久 | 亚洲va久久久噜噜噜久久男同 | 久久精品国产久精国产一老狼| 久久99精品国产自在现线小黄鸭 | 久久只有这精品99| 久久精品国产一区二区| 狠狠色丁香久久婷婷综合五月 | 97超级碰碰碰碰久久久久| 久久久久精品国产亚洲AV无码| 久久国产精品免费一区二区三区| 少妇久久久久久被弄高潮| 麻豆精品久久久久久久99蜜桃| 污污内射久久一区二区欧美日韩| 久久久久久国产精品免费无码| 四虎国产精品成人免费久久| 久久五月精品中文字幕| 国产激情久久久久影院| 精品久久久久一区二区三区| 久久精品夜夜夜夜夜久久| 久久妇女高潮几次MBA| 婷婷国产天堂久久综合五月| 亚洲七七久久精品中文国产| 亚洲欧美日韩精品久久亚洲区 | 久久久精品免费国产四虎|