Demo entry 6357661


Submitted by anonymous on Apr 23, 2017 at 15:42
Language: Python 3. Code size: 3.0 kB.

# -*- coding: utf-8 -*-
import sys

import os
import re
import poplib
import base64
import urllib2
from email.parser import Parser
from email.header import decode_header

class Inbox:
    def __init__(self):
        self.conn = poplib.POP3_SSL('')
        self.conn.pass_('xxxxxx')  # your pop keys
        self.mails = []

    def get_charset(self, msg):
        charset = msg.get_charset()
        if charset is None:
            content_type = msg.get('Content-Type', '').lower()
            pos = content_type.find('charset=')
            if pos >= 0:
                charset = content_type[pos + 8:].strip()
        return charset

    def decode_str(self, s):
        value, charset = decode_header(s)[0]
        if charset:
            value = value.decode(charset)
        return value

    def fetch(self):
        resp, mails, octets = self.conn.uidl()
        mail_ids = [mail.split(' ')[1] for mail in mails]
        for mail_id in mail_ids:
            pos = mail_ids.index(mail_id) + 1
            resp, lines, octets = self.conn.retr(pos)
        for mail in self.mails:

    def down(self, mail):
        msg = Parser().parsestr(mail)
        for part in msg.walk():
            if not part.is_multipart() and part.get_content_type() == 'application/octet-stream':
                filename = self.decode_str(part.get_filename())
                with open(r'down/%s' % filename, 'wb') as f:
                    print('down %s done.' % filename)
            elif not part.is_multipart() and part.get_content_type() in ('text/plain', 'text/html'):
                    content = part.get_payload(decode=True)
                    charset = self.get_charset(part)
                    if charset:
                        content = content.decode(charset)
                    if r'ftnExs_download' in content:
                        pattern = re.compile(ur'([\S]+).*\(.*到期\).*进入下载页面:(\S+)')
                        match = pattern.findall(content)
                        if match:

    def down_ftnexs(self, ftnexs):
        for filename, rurl in ftnexs:
            pattern = re.compile(ur'<a un="down".*?href="(?P<url>\S+)" target=')
            match =
            if match:
                url = match.groupdict().get('url').strip()
                with open(r'down/%s' % filename, 'wb') as f:
                    print('down %s done.' % filename)

if __name__ == '__main__':
    poplib._MAXLINE = 20480
    if not os.path.exists('down'):
    inbox = Inbox()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).