<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
        主站蜘蛛池模板: 无码专区一va亚洲v专区在线 | 免费精品一区二区三区第35| 中文字幕无码日韩专区免费| 国产免费丝袜调教视频| 国产免费看插插插视频| 91在线精品亚洲一区二区| 人成午夜免费大片在线观看| 色妞WWW精品免费视频| 人人狠狠综合久久亚洲婷婷| 亚洲性色AV日韩在线观看| a毛片在线还看免费网站| 日本人护士免费xxxx视频| 亚洲高清日韩精品第一区| 青草草色A免费观看在线| 久久精品夜色国产亚洲av| 美女被爆羞羞网站免费| 女人张腿给男人桶视频免费版| 久久久亚洲精品视频| 日韩免费在线中文字幕| 亚洲乱码中文字幕久久孕妇黑人| 亚洲精品成人网站在线播放| 中文字幕的电影免费网站| 又黄又爽一线毛片免费观看 | aⅴ在线免费观看| 久久亚洲综合色一区二区三区| 免费看又黄又无码的网站| 伊人久久精品亚洲午夜| 爱情岛论坛免费视频| 亚洲人成色77777| 日韩在线免费视频| 鲁啊鲁在线视频免费播放| 亚洲爆乳无码一区二区三区| 曰批视频免费40分钟试看天天| 久久亚洲国产伦理| 无码少妇一区二区浪潮免费| 亚洲日本香蕉视频| h视频在线观看免费完整版| 亚洲av成本人无码网站| 国产免费av片在线播放| 伊人久久大香线蕉免费视频| 亚洲 欧洲 自拍 另类 校园|