本文整理汇总了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)