本文整理匯總了Python中email.parser.BytesParser方法的典型用法代碼示例。如果您正苦於以下問題:Python parser.BytesParser方法的具體用法?Python parser.BytesParser怎麽用?Python parser.BytesParser使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類email.parser
的用法示例。
在下文中一共展示了parser.BytesParser方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: messages
# 需要導入模塊: from email import parser [as 別名]
# 或者: from email.parser import BytesParser [as 別名]
def messages(self, folder, limit=10, peek=True):
"""Return ``limit`` messages from ``folder``
peek=False will also fetch message body
"""
resp, count = self._imap.select('"%s"' % folder, readonly=True)
if resp != 'OK':
raise Exception(resp)
last_message_id = int(count[0])
msg_ids = range(last_message_id, last_message_id-limit, -1)
mode = '(BODY.PEEK[HEADER])' if peek else '(RFC822)'
messages = []
for msg_id in msg_ids:
resp, msg = self._imap.fetch(str(msg_id), mode)
msg = msg[0][-1]
messages.append(BytesParser().parsebytes(msg))
if len(messages) >= limit:
break
return messages
示例2: get_metadata_for_wheel
# 需要導入模塊: from email import parser [as 別名]
# 或者: from email.parser import BytesParser [as 別名]
def get_metadata_for_wheel(url):
data = requests.get(url).content
with ZipFile(BytesIO(data)) as z:
for n in z.namelist():
if n.endswith(".dist-info/METADATA"):
p = BytesParser()
return p.parse(z.open(n), headersonly=True)
# If we didn't find the metadata, return an empty dict
return EmailMessage()
示例3: message_from_bytes
# 需要導入模塊: from email import parser [as 別名]
# 或者: from email.parser import BytesParser [as 別名]
def message_from_bytes(s, *args, **kws):
"""Parse a bytes string into a Message object model.
Optional _class and strict are passed to the Parser constructor.
"""
from email.parser import BytesParser
return BytesParser(*args, **kws).parsebytes(s)
示例4: message_from_binary_file
# 需要導入模塊: from email import parser [as 別名]
# 或者: from email.parser import BytesParser [as 別名]
def message_from_binary_file(fp, *args, **kws):
"""Read a binary file and parse its contents into a Message object model.
Optional _class and strict are passed to the Parser constructor.
"""
from email.parser import BytesParser
return BytesParser(*args, **kws).parse(fp)
示例5: process_email
# 需要導入模塊: from email import parser [as 別名]
# 或者: from email.parser import BytesParser [as 別名]
def process_email(raw_email):
msg = BytesParser(policy=policy.default).parsebytes(raw_email)
body = msg.get_body(preferencelist=['plain'])
content = body.get_payload(decode=True)
charset = body.get_content_charset()
if not charset:
charset = chardet.detect(content)['encoding']
content = content.decode(charset)
regex = re.compile('^[^+@]+\+(?P<token>[a-zA-Z0-9]{80})@[^@]+$')
for addr in msg.get('To', '').split(','):
m = regex.match(addr.strip())
if m:
break
if not m:
raise NoTokenFoundException
token = m.group('token')
try:
in_reply_to, author = process_new_token(token)
except InvalidTokenException:
in_reply_to, author = process_old_token(token)
subject = msg.get('Subject', '')
Message.objects.create(thread=in_reply_to.thread, in_reply_to=in_reply_to, author=author, subject=subject, content=content)