最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python編程實(shí)現(xiàn)及時(shí)獲取新郵件的方法示例

 更新時(shí)間:2017年08月10日 12:21:20   作者:liumengcheng  
這篇文章主要介紹了Python編程實(shí)現(xiàn)及時(shí)獲取新郵件的方法,涉及Python實(shí)時(shí)查詢郵箱及郵件獲取相關(guān)操作技巧,需要的朋友可以參考下

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

#-*- 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');
# 確定運(yùn)行環(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 = ["\n".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ù)庫(kù)
conn2 = mysql.connector.connect(user='root', password='password',host='127.0.0.1',database='hmailserver',charset='gb2312');
cur2 = conn2.cursor();
#只要收到電子郵件,就把這個(gè)事件記錄在事件庫(kù)中
#現(xiàn)在就是循環(huán)查詢郵箱,如果有新郵件就讀取,并查詢關(guān)鍵詞庫(kù)
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

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 解決pyinstaller打包pyqt5的問題

    解決pyinstaller打包pyqt5的問題

    今天小編就為大家分享一篇解決pyinstaller打包pyqt5的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python?Scrapy庫(kù)構(gòu)建基礎(chǔ)爬蟲

    Python?Scrapy庫(kù)構(gòu)建基礎(chǔ)爬蟲

    這篇文章主要為大家介紹了Python?Scrapy庫(kù)構(gòu)建基礎(chǔ)爬蟲示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • python實(shí)現(xiàn)MongoDB的雙活示例

    python實(shí)現(xiàn)MongoDB的雙活示例

    本文主要介紹了python實(shí)現(xiàn)MongoDB的雙活示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python request操作步驟及代碼實(shí)例

    Python request操作步驟及代碼實(shí)例

    這篇文章主要介紹了Python request操作步驟及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼

    Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Django 自定義分頁(yè)器的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python?Matplotlib繪制箱線圖boxplot()函數(shù)詳解

    Python?Matplotlib繪制箱線圖boxplot()函數(shù)詳解

    箱線圖一般用來展現(xiàn)數(shù)據(jù)的分布(如上下四分位值、中位數(shù)等),同時(shí)也可以用箱線圖來反映數(shù)據(jù)的異常情況,下面這篇文章主要給大家介紹了關(guān)于Python?Matplotlib繪制箱線圖boxplot()函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • python如何創(chuàng)建TCP服務(wù)端和客戶端

    python如何創(chuàng)建TCP服務(wù)端和客戶端

    這篇文章主要為大家詳細(xì)介紹了python如何創(chuàng)建TCP服務(wù)端和客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Python安裝.whl文件流程以及問題解決方法

    Python安裝.whl文件流程以及問題解決方法

    WHL文件是以Wheel格式保存的Python安裝包,Wheel是Python發(fā)行版的標(biāo)準(zhǔn)內(nèi)置包格式,下面這篇文章主要給大家介紹了關(guān)于Python安裝.whl文件流程以及問題解決方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Flask快速實(shí)現(xiàn)分頁(yè)效果示例

    Flask快速實(shí)現(xiàn)分頁(yè)效果示例

    本文主要介紹了Flask快速實(shí)現(xiàn)分頁(yè)效果示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • django之常用命令詳解

    django之常用命令詳解

    下面小編就為大家?guī)硪黄猟jango之常用命令詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06

最新評(píng)論

都昌县| 乌兰浩特市| 丹东市| 康定县| 沧源| 新田县| 安多县| 绥芬河市| 鸡西市| 皋兰县| 高安市| 浮山县| 眉山市| 徐水县| 沙坪坝区| 苏尼特左旗| 聊城市| 昭平县| 建水县| 滕州市| 隆化县| 敖汉旗| 德化县| 富蕴县| 西乌珠穆沁旗| 正宁县| 曲靖市| 巨鹿县| 娄烦县| 山丹县| 蕉岭县| 利辛县| 长乐市| 响水县| 华容县| 平定县| 旌德县| 比如县| 衡南县| 滁州市| 巴塘县|