Patch for issue 2848, mostly by Humberto Diogenes, with a couple of
small fixes by Barry.  This removes mimetools from the stdlib.
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
index 09fb0c6..c130190 100644
--- a/Lib/test/test_http_cookiejar.py
+++ b/Lib/test/test_http_cookiejar.py
@@ -193,9 +193,8 @@
         """
         headers: list of RFC822-style 'Key: value' strings
         """
-        import mimetools, io
-        f = io.StringIO("\n".join(headers))
-        self._headers = mimetools.Message(f)
+        import email
+        self._headers = email.message_from_string("\n".join(headers))
         self._url = url
     def info(self): return self._headers
 
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
index 02be57a..94c6a3d 100644
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -19,12 +19,14 @@
 import unittest
 from test import support
 
-
 class NoLogRequestHandler:
     def log_message(self, *args):
         # don't write log messages to stderr
         pass
 
+    def read(self, n=None):
+        return ''
+
 
 class TestServerThread(threading.Thread):
     def __init__(self, test_object, request_handler):
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 33419fe..619161e 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -944,7 +944,7 @@
                 url = 'https://%s:%d/%s' % (
                     HOST, server.port, os.path.split(CERTFILE)[1])
                 f = urllib.urlopen(url)
-                dlen = f.info().getheader("content-length")
+                dlen = f.info().get("content-length")
                 if dlen and (int(dlen) > 0):
                     d2 = f.read(int(dlen))
                     if support.verbose:
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 26b51cb..2b41cad 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -2,11 +2,11 @@
 
 import urllib
 import http.client
+import email.message
 import io
 import unittest
 from test import support
 import os
-import mimetools
 import tempfile
 
 def hexescape(char):
@@ -78,7 +78,7 @@
         self.returned_obj.close()
 
     def test_info(self):
-        self.assert_(isinstance(self.returned_obj.info(), mimetools.Message))
+        self.assert_(isinstance(self.returned_obj.info(), email.message.Message))
 
     def test_geturl(self):
         self.assertEqual(self.returned_obj.geturl(), self.pathname)
@@ -206,8 +206,8 @@
         # a headers value is returned.
         result = urllib.urlretrieve("file:%s" % support.TESTFN)
         self.assertEqual(result[0], support.TESTFN)
-        self.assert_(isinstance(result[1], mimetools.Message),
-                     "did not get a mimetools.Message instance as second "
+        self.assert_(isinstance(result[1], email.message.Message),
+                     "did not get a email.message.Message instance as second "
                      "returned value")
 
     def test_copy(self):
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index c2d594b..6bcb17e 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -349,18 +349,18 @@
         self._count = 0
         self.requests = []
     def http_open(self, req):
-        import mimetools, http.client, copy
+        import email, http.client, copy
         from io import StringIO
         self.requests.append(copy.deepcopy(req))
         if self._count == 0:
             self._count = self._count + 1
             name = http.client.responses[self.code]
-            msg = mimetools.Message(StringIO(self.headers))
+            msg = email.message_from_string(self.headers)
             return self.parent.error(
                 "http", req, MockFile(), self.code, name, msg)
         else:
             self.req = req
-            msg = mimetools.Message(StringIO("\r\n\r\n"))
+            msg = email.message_from_string("\r\n\r\n")
             return MockResponse(200, "OK", msg, "", req.get_full_url())
 
 class MockPasswordManager:
diff --git a/Lib/test/test_urllib2_localnet.py b/Lib/test/test_urllib2_localnet.py
index 6c877d9..d3016c3 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-import mimetools
+import email
 import threading
 import urlparse
 import urllib2
@@ -443,10 +443,10 @@
         try:
             open_url = urllib2.urlopen("http://localhost:%s" % handler.port)
             info_obj = open_url.info()
-            self.assert_(isinstance(info_obj, mimetools.Message),
+            self.assert_(isinstance(info_obj, email.message.Message),
                          "object returned by 'info' is not an instance of "
-                         "mimetools.Message")
-            self.assertEqual(info_obj.getsubtype(), "plain")
+                         "email.message.Message")
+            self.assertEqual(info_obj.get_content_subtype(), "plain")
         finally:
             self.server.stop()
 
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index 1c4af18..938ab9f 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -8,7 +8,6 @@
 import urllib2
 import sys
 import os
-import mimetools
 
 
 def _retry_thrice(func, exc, *args, **kwargs):
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index 6c2632c..d4831d8 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -7,7 +7,7 @@
 import urllib
 import sys
 import os
-import mimetools
+import email.message
 
 
 def _open_with_retry(func, host, *args, **kwargs):
@@ -87,10 +87,10 @@
             info_obj = open_url.info()
         finally:
             open_url.close()
-            self.assert_(isinstance(info_obj, mimetools.Message),
+            self.assert_(isinstance(info_obj, email.message.Message),
                          "object returned by 'info' is not an instance of "
-                         "mimetools.Message")
-            self.assertEqual(info_obj.getsubtype(), "html")
+                         "email.message.Message")
+            self.assertEqual(info_obj.get_content_subtype(), "html")
 
     def test_geturl(self):
         # Make sure same URL as opened is returned by geturl.
@@ -180,8 +180,8 @@
         # Make sure header returned as 2nd value from urlretrieve is good.
         file_location, header = self.urlretrieve("http://www.python.org/")
         os.unlink(file_location)
-        self.assert_(isinstance(header, mimetools.Message),
-                     "header is not an instance of mimetools.Message")
+        self.assert_(isinstance(header, email.message.Message),
+                     "header is not an instance of email.message.Message")
 
 
 
diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
index b9103fa..9d38470 100644
--- a/Lib/test/test_xmlrpc.py
+++ b/Lib/test/test_xmlrpc.py
@@ -6,7 +6,6 @@
 import xmlrpc.client as xmlrpclib
 import xmlrpc.server
 import threading
-import mimetools
 import http.client
 import socket
 import os
@@ -452,12 +451,12 @@
 
 # This is a contrived way to make a failure occur on the server side
 # in order to test the _send_traceback_header flag on the server
-class FailingMessageClass(mimetools.Message):
-    def __getitem__(self, key):
+class FailingMessageClass(http.client.HTTPMessage):
+    def get(self, key, failobj=None):
         key = key.lower()
         if key == 'content-length':
             return 'I am broken'
-        return mimetools.Message.__getitem__(self, key)
+        return super().get(key, failobj)
 
 
 class FailingServerTestCase(unittest.TestCase):
@@ -477,7 +476,8 @@
         # reset flag
         xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = False
         # reset message class
-        xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = mimetools.Message
+        default_class = http.client.HTTPMessage
+        xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = default_class
 
     def test_basic(self):
         # check that flag is false by default
@@ -529,8 +529,8 @@
             if not is_unavailable_exception(e) and hasattr(e, "headers"):
                 # We should get error info in the response
                 expected_err = "invalid literal for int() with base 10: 'I am broken'"
-                self.assertEqual(e.headers.get("x-exception"), expected_err)
-                self.assertTrue(e.headers.get("x-traceback") is not None)
+                self.assertEqual(e.headers.get("X-exception"), expected_err)
+                self.assertTrue(e.headers.get("X-traceback") is not None)
         else:
             self.fail('ProtocolError not raised')