当前位置: 首页>>代码示例>>Python>>正文


Python Gmail.get_headers方法代码示例

本文整理汇总了Python中gmail.Gmail.get_headers方法的典型用法代码示例。如果您正苦于以下问题:Python Gmail.get_headers方法的具体用法?Python Gmail.get_headers怎么用?Python Gmail.get_headers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gmail.Gmail的用法示例。


在下文中一共展示了Gmail.get_headers方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CommandHandler

# 需要导入模块: from gmail import Gmail [as 别名]
# 或者: from gmail.Gmail import get_headers [as 别名]
class CommandHandler(object):

    def __init__(self):
        self.gpg = None
        self.gmail = None
        self.gpgmail = None
        self.initialized = False
        self.logger = logging.getLogger('CommandHandler')

    def parse(self, bundle):
        if not 'command' in bundle:
            self.logger.error("no command in bundle {}".format(bundle))
            return None
        result = None
        command = bundle["command"]
        if command == 'init':
            result = self.init(bundle)
        else:
            # do nothing if not intialized
            if not self.initialized:
                return False
            if command == 'verify':
                result = self.verify(bundle)
            elif command == 'sign':
                result = self.sign(bundle)
            elif command == 'import':
                result = self.import_key(bundle)
        return result

    def init(self, bundle):
        if not 'username' in bundle:
            return False
        self.gpg = GPG(use_agent=True)
        self.gpgmail = GPGMail(self.gpg)
        self.gmail = Gmail(bundle['username'])
        self.initialized = True
        return {'version': VERSION}

    def verify(self, bundle):
        if not 'id' in bundle:
            return False

        id = bundle['id']

        def _verify():
            mail = self.gmail.get(id)
            return self.gpgmail.verify(mail)

        # verify the message only if bundle['force'] = true
        if 'force' in bundle and bundle['force']:
            return _verify()

        # or content_type of message is 'multipart/signed'
        headers = self.gmail.get_headers(id, ['Content-Type', 'Message-ID'])
        if 'Content-Type' in headers:
            content_type = headers['Content-Type']
            if content_type.find('multipart/signed') >= 0:
                return _verify()

            # or if message is multipart and it may contain a pgp-signature
            is_multipart = content_type.find('multipart/') >= 0
            if (is_multipart and 'Message-ID' in headers):
                rfc822msgid = headers['Message-ID']
                query = '("BEGIN PGP SIGNATURE" "END PGP SIGNATURE")'
                match = self.gmail.message_matches(id, query, rfc822msgid)
                if match:
                    return _verify()
        # else
        return None

    def sign(self, bundle):
        if not 'id' in bundle:
            return False
        result = False
        id = bundle['id']
        try:
            draft = self.gmail.get(id)
            new_message = self.gpgmail.sign(draft)
            if new_message:
                self.gmail.send(id, new_message)
                result = True
        except Exception, e:
            self.logger.exception(e)
        finally:
开发者ID:jubayerarefin,项目名称:goopg,代码行数:86,代码来源:commandhandler.py


注:本文中的gmail.Gmail.get_headers方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。