def parse_mime_headers(doc_file):
 headers = []
 while True:
  line = doc_file.readline()
  done = (line in ('\r\n', '\n', ''))
  if six.PY3:
   try:
    line = line.decode('utf-8')
   except UnicodeDecodeError:
    line = line.decode('latin1')
  headers.append(line)
  if done:
   break
 if six.PY3:
  header_string = ''.join(headers)
 else:
  header_string = ''.join(headers)
 headers = email.parser.Parser().parsestr(header_string)
 return HeaderKeyDict(headers)

def parse_mime_headers ( doc_file ) : headers = [ ] while True : line = doc_file . readline ( ) done = ( line in ( '\r\n' , '\n' , '' ) ) if six . PY3 : try : line = line . decode ( 'utf-8' ) except UnicodeDecodeError : line = line . decode ( 'latin1' ) headers . append ( line ) if done : break if six . PY3 : header_string = '' . join ( headers ) else : header_string = '' . join ( headers ) headers = email . parser . Parser ( ) . parsestr ( header_string ) return HeaderKeyDict ( headers )
takes a file-like object containing a mime document and returns a headerkeydict containing the headers .