<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
        主站蜘蛛池模板: 很黄很色很刺激的视频免费| 黄a大片av永久免费| 亚洲黄色免费电影| 国产成人免费a在线视频app| 亚洲综合色婷婷七月丁香| 亚洲av无码电影网| 中文字幕乱码免费看电影| 成人毛片免费视频| 老司机亚洲精品影院无码| 国产精品1024在线永久免费| 四虎成人精品一区二区免费网站| 亚洲AV永久无码精品一百度影院| 激情无码亚洲一区二区三区 | 久久精品国产亚洲AV大全| 精品无码无人网站免费视频| 久久亚洲中文字幕精品一区四 | 韩国18福利视频免费观看| 亚洲欧美日韩综合久久久| 亚洲人成在线免费观看| 亚洲AV无码乱码在线观看富二代| 日韩精品免费视频| 亚洲色大成网站www永久一区| 免费精品国产自产拍在线观看| 成年女人喷潮毛片免费播放| 激情小说亚洲图片| 亚洲国产精品无码成人片久久| 久久av免费天堂小草播放| 亚洲福利精品一区二区三区 | 久久久久亚洲AV无码麻豆| 日韩毛片免费无码无毒视频观看| 亚洲视频在线视频| 四虎国产精品永久免费网址 | 四虎精品免费永久免费视频| 国产乱子伦精品免费无码专区| 亚洲高清中文字幕免费| www.999精品视频观看免费| 亚洲午夜精品在线| 在线观看的免费网站| 无遮挡呻吟娇喘视频免费播放| 亚洲国产精品免费视频| 免费真实播放国产乱子伦|