def parse1(request_text=None, file_path=None): # TODO: 如果提供了file_path, 优先filepath if file_path: request_text = ParseReqHeader().get_request_text_by_file(file_path) request_line, headers_alone = request_text.split(b'\r\n', 1) headers = BytesParser().parsebytes(headers_alone) return {k: v for k, v in headers.items()}
def _get_parts_regular(self, data): msg = BytesParser().parsebytes(data) yield from self._get_headparts(msg.items()) for part in msg.walk(): path = part.get_filename() data = part.get_payload(decode=True) if data is None: continue if path is None: path = F'BODY.{file_extension(part.get_content_subtype(), "TXT").upper()}' yield UnpackResult(path, data)
def _get_parts_regular(self, data): if not re.match(BR'^[\s!-~]+$', data): raise ValueError('This is not a plaintext email message.') msg = BytesParser().parsebytes(data) yield from self._get_headparts(msg.items()) for k, part in enumerate(msg.walk()): path = part.get_filename() elog = None if path is None: extension = file_extension(part.get_content_type(), 'txt') path = F'body.{extension}' else: path = F'attachments/{path}' try: data = part.get_payload(decode=True) except Exception as E: try: data = part.get_payload(decode=False) except Exception as E: elog = str(E) data = None else: from refinery import carve self.log_warn(F'manually decoding part {k}, data might be corrupted: {path}') if isinstance(data, str): data = data.encode('latin1') if isbuffer(data): data = next(data | carve('b64', stripspace=True, single=True, decode=True)) else: elog = str(E) data = None if not data: if elog is not None: self.log_warn(F'could not get content of message part {k}: {elog!s}') continue yield UnpackResult(path, data)
def extractMetaData(self,obj): headers = BytesParser().parse(obj) h = dict(headers.items()) return self.convertMetaDataToSwiftFormat(h)
def extractMetaData(self, obj): headers = BytesParser().parse(obj) metadata = dict(headers.items()) return self.cleanupMetaDataDict(metadata)