国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答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
主站蜘蛛池模板: 国内精品伊人久久久久妇 | 精品一区二区三区在线观看 | 亚洲国产欧美91 | 成人毛片一区二区三区 | 国产欧美一区二区精品久久久 | 亚洲国产精品一区二区久 | 永久免费观看的毛片的网站下载 | 欧美日韩国产高清一区二区三区 | 香港经典a毛片免费观看…伊人色综合久久 | 日韩国产一区二区 | 免费一级a毛片在线播 | 免费国产va在线观看视频 | 久久精品无遮挡一级毛片 | 国产欧美视频在线观看 | 中文字幕一区二区三区在线观看 | 日韩综合图区 | 国产亚洲午夜精品a一区二区 | 国产产一区二区三区久久毛片国语 | 精品久久久久久久久中文字幕 | 亚洲欧美日韩在线2020 | 久国产精品视频 | 亚洲人成一区 | 日韩不卡一区 | 日韩二区三区 | 天堂va欧美ⅴa亚洲va一国产 | 国产日韩亚洲欧美 | 香蕉久久网| 美女视频黄a视频全免费网站色 | 免费成人毛片 | 午夜黄色在线观看 | 国内一区亚洲综合图区欧美 | 99国产精品久久久久久久成人热 | 日韩欧美在线观看视频 | 天堂va欧美ⅴa亚洲va一国产 | 欧美日韩亚洲综合另类ac | 国产成人成人一区二区 | 劲爆欧美第一页 | 欧美在线日韩在线 | 国产精品福利久久久久久小说 | 最新偷窥盗摄视频在线 | 国产一级特黄高清免费大片dvd |