• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答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實現獲取及時郵件的方法

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

    Python實現獲取及時郵件的方法

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

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

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

    #-*- 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');
    # 確定運行環境的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;
    #全局已讀的郵件數量
    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
    #連接數據庫
    conn2 = mysql.connector.connect(user='root', password='password',host='127.0.0.1',database='hmailserver',charset='gb2312');
    cur2 = conn2.cursor();
    #只要收到電子郵件,就把這個事件記錄在事件庫中
    #現在就是循環查詢郵箱,如果有新郵件就讀取,并查詢關鍵詞庫
    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

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

    文檔

    Python實現獲取及時郵件的方法

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

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 你懂的国产精品| 精品久久人人爽天天玩人人妻| 国产精品亚洲欧美一区麻豆| 久久se精品一区精品二区国产| 国产精品99久久精品| 一区二区日韩国产精品| 国产精品综合久成人| 久久99国产精品久久久| 国产综合精品女在线观看| 亚洲欧美日韩精品专区| 久久久久国产精品三级网| 国产成人精品久久亚洲高清不卡| 999在线视频精品免费播放观看| 亚洲精品美女久久久久99| 欧美精品黑人粗大欧| 国产精品日韩欧美久久综合| 四虎成人精品免费影院| 国产亚洲一区二区精品| 国产精品免费看久久久| 精品人妻久久久久久888| 亚洲Av永久无码精品三区在线| 免费看一级毛片在线观看精品视频 | 亚洲精品国产成人专区| 成人午夜视频精品一区| 国产在线精品一区二区中文| 久久精品亚洲一区二区三区浴池| 亚洲AV永久青草无码精品| 亚洲国产精品专区在线观看| 久久精品无码一区二区三区免费| 国产这里有精品| 精品精品国产欧美在线小说区| 国产精品综合久成人| 精品久久久久久国产牛牛app| 久久精品国产精品亜洲毛片| 国产亚洲精品成人a v小说| 精品久久久无码中文字幕天天| 精品无人区无码乱码大片国产| 久久国产成人亚洲精品影院| 欧美日韩精品在线观看| 亚洲精品美女久久久久99小说| 亚洲精品综合久久|