<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Python實現(xiàn)獲取及時郵件的方法

        來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 14:23:15
        文檔

        Python實現(xiàn)獲取及時郵件的方法

        Python實現(xiàn)獲取及時郵件的方法:這篇文章主要介紹了Python編程實現(xiàn)及時獲取新郵件的方法,涉及Python實時查詢郵箱及郵件獲取相關操作技巧,需要的朋友可以參考下本文實例講述了Python編程實現(xiàn)及時獲取新郵件的方法。分享給大家供大家參考,具體如下:#-*- encoding: utf-8 -*-
        推薦度:
        導讀Python實現(xiàn)獲取及時郵件的方法:這篇文章主要介紹了Python編程實現(xiàn)及時獲取新郵件的方法,涉及Python實時查詢郵箱及郵件獲取相關操作技巧,需要的朋友可以參考下本文實例講述了Python編程實現(xiàn)及時獲取新郵件的方法。分享給大家供大家參考,具體如下:#-*- encoding: utf-8 -*-

        這篇文章主要介紹了Python編程實現(xiàn)及時獲取新郵件的方法,涉及Python實時查詢郵箱及郵件獲取相關操作技巧,需要的朋友可以參考下

        本文實例講述了Python編程實現(xiàn)及時獲取新郵件的方法。分享給大家供大家參考,具體如下:

        #-*- encoding: utf-8 -*-
        import sys
        import locale
        import poplib
        from email import parser
        import email
        import string
        import mysql.connector
        import traceback
        import datetime
        from mysql.connector import errorcode
        import time
        import re
        reload(sys);
        sys.setdefaultencoding('utf8');
        # 確定運行環(huán)境的encoding
        __g_codeset = sys.getdefaultencoding()
        if "ascii"==__g_codeset:
         __g_codeset = 'utf8';
        #
        def object2double(obj):
         if(obj==None or obj==""):
         return 0
         else:
         return float(obj)
         #end if
        #
        def getMailIndex():
         file = open('mailindex.txt',"r");
         lines = file.readlines();
         file.close();
         return int(lines[0]);
        #
        def setMailIndex(index):
         f = open('mailindex.txt', 'w');
         f.write(index);
         f.close();
        #
        def utf8_to_mbs(s):
         return s.decode("utf-8").encode(__g_codeset)
        #
        def utf8_to_gbk(s):
         return s.decode("utf-8").encode('gb2312')
        #
        def mbs_to_utf8(s):
         return s.decode(__g_codeset).encode("utf-8")
        #
        def gbk_to_utf8(s):
         return s.decode('gb2312').encode("utf-8")
        #
        def _queryQuick(cu,sql,tuple):
         try:
         cu.execute(sql,tuple);
         rows = []
         for row in cu:
         rows.append(row)
         #
         return rows
         except:
         print(traceback.format_exc())
         #end
        #
        #獲取信息
        def _queryRows(cu,sql):
         try:
         cu.execute(sql)
         rows = []
         for row in cu:
         rows.append(row)
         #
         return rows
         except:
         print(traceback.format_exc())
         #end
        #
        #是否有新郵件
        global hasNewMail;
        hasNewMail=True;
        #全局已讀的郵件數(shù)量
        global globalMailReaded;
        globalMailReaded=getMailIndex()+1;
        #獲取新郵件
        def getNewMail(conn2,cur2):
         try:
         global hasNewMail;
         global globalMailReaded;
         conn2.commit();
         rows=_queryRows(cur2,"select count(*) as message_count from hm_messages where messageaccountid=1");
         message_count=rows[0][0];
         if(hasNewMail):
         print('read mailindex.txt')
         globalMailReaded=getMailIndex()+1;
         #end if
         if(message_count<=globalMailReaded):
         hasNewMail=False;
         #print('Did not receive new mail,continue wait...')
         return None;#沒新郵件,直接返回
         #end if
         #登陸郵箱
         host = '127.0.0.1'
         username = 'username@myserver.net'
         password = 'password'
         pop_conn = poplib.POP3(host)
         #print pop_conn.getwelcome()
         pop_conn.user(username);
         pop_conn.pass_(password);
         #Get messages from server:
         messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
         # Concat message pieces:
         messages = ["
        ".join(mssg[1]) for mssg in messages]
         #Parse message intom an email object:
         messages = [parser.Parser().parsestr(mssg) for mssg in messages]
         print("get new mail!");
         print pop_conn.stat()
         print('%s readed mail count is %d,all mail count is: %d'%(datetime.datetime.now().strftime("%y-%m-%d %H:%M:%S"),globalMailReaded,len(messages)))
         message = messages[globalMailReaded];
         subject = message.get('subject')
         h = email.Header.Header(subject)
         dh = email.Header.decode_header(h)
         #subject = unicode(dh[0][0], dh[0][1]).encode('utf8')
         #print >> f, "Date: ", message["Date"]
         #print >> f, "From: ", email.utils.parseaddr(message.get('from'))[1]
         #print >> f, "To: ", email.utils.parseaddr(message.get('to'))[1]
         #print >> f, "Subject: ", subject
         j = 0
         for part in message.walk():
         j = j + 1
         fileName = part.get_filename()
         contentType = part.get_content_type()
         mycode=part.get_content_charset();
         # 保存附件
         if fileName:
         pass;
         elif contentType == 'text/plain':# or contentType == 'text/html':
         #保存正文
         data = part.get_payload(decode=True)
         content=str(data);
         if mycode=='gb2312':
         content= gbk_to_utf8(content)
         #end if
         content=content.replace(u'u200d','');
         setMailIndex(str(globalMailReaded));
         hasNewMail=True;
         pop_conn.quit();
         return (content,email.utils.parseaddr(message.get('from'))[1]);
         #end if
         #end for
         except:
         print("search hmailserver fail,try again");
         return None;
         finally:
         pass;
         #end try
        #end def
        #連接數(shù)據(jù)庫
        conn2 = mysql.connector.connect(user='root', password='password',host='127.0.0.1',database='hmailserver',charset='gb2312');
        cur2 = conn2.cursor();
        #只要收到電子郵件,就把這個事件記錄在事件庫中
        #現(xiàn)在就是循環(huán)查詢郵箱,如果有新郵件就讀取,并查詢關鍵詞庫
        while(True):
         mailtuple=getNewMail(conn2,cur2);
         if(mailtuple==None):
         #print('Did not search MySQL,continue loop...')
         time.sleep(0.5)
         continue;
         #end if
         (article,origin)=mailtuple;
        #end while

        聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Python實現(xiàn)獲取及時郵件的方法

        Python實現(xiàn)獲取及時郵件的方法:這篇文章主要介紹了Python編程實現(xiàn)及時獲取新郵件的方法,涉及Python實時查詢郵箱及郵件獲取相關操作技巧,需要的朋友可以參考下本文實例講述了Python編程實現(xiàn)及時獲取新郵件的方法。分享給大家供大家參考,具體如下:#-*- encoding: utf-8 -*-
        推薦度:
        標簽: 郵件 獲取 代碼
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲国产综合专区电影在线| 亚洲综合久久夜AV | 亚洲情a成黄在线观看动漫尤物| 精品97国产免费人成视频| 亚洲国产精品丝袜在线观看| 免费精品视频在线| 国产亚洲精品精品国产亚洲综合 | 国产午夜亚洲精品午夜鲁丝片| 免费人成视频在线观看免费| 亚洲日韩国产一区二区三区| 亚洲高清免费视频| 久久精品国产亚洲av麻| 四虎永久在线精品免费观看视频| 国产日本亚洲一区二区三区| 日韩特黄特色大片免费视频| 日韩电影免费在线观看网址 | 久久久久久久久亚洲| 免费国产黄网站在线观看| 亚洲国产美女精品久久| 韩国18福利视频免费观看| 免费的黄色网页在线免费观看| 亚洲精品字幕在线观看| 亚洲精品在线免费观看视频| 亚洲av日韩av永久无码电影| 亚洲国产精品无码久久青草 | 亚洲午夜无码久久久久软件| 国产免费观看视频| 成全视频免费观看在线看| 亚洲国产综合第一精品小说| 免费在线观看中文字幕| 亚欧免费无码aⅴ在线观看| 亚洲国产成人精品激情| 亚洲午夜精品久久久久久浪潮 | 国拍在线精品视频免费观看| 亚洲国产成人AV网站| 亚洲日韩图片专区第1页| 国产成人精品免费视频大全五级| 成在人线av无码免费高潮喷水| 亚洲午夜一区二区电影院| 亚洲欧洲中文日韩久久AV乱码| 久久精品视频免费看|