Issue 1:
On the topic of requests not mapping exceptions to its own exceptions...

requests.packages.urllib3.exceptions.ProtocolError is not caught. Reproduction: invalid status line.

Server:

$ echo | nc -l 8000

requests:

import requests
try:
requests.get('http://localhost:8000')
except Exception as e:
print (e.__class__)

Output: <class 'requests.packages.urllib3.exceptions.ProtocolError'>

I would expect this to be mapped to a requests exception.

Connection refused comes out also as ProtocolError.

This is requests 2.4.0.

Ping @Lukasa , @sigmavirus24

Also happens with connection refused. This should be covered by requests unittests in the future.

Traceback (most recent call last):
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 516, in urlopen
body=body, headers=headers)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 308, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1086, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 924, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 859, in send
self.connect()
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 154, in connect
conn = self._new_conn()
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 133, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 87, in create_connection
raise err
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 78, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<ipython-input-14-1e96a12a07a3>", line 1, in <module>
try: requests.get('http://localhost:8000')
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/sessions.py", line 448, in request
resp = self.send(prep, **send_kwargs)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/sessions.py", line 554, in send
r = adapter.send(request, **kwargs)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/adapters.py", line 359, in send
timeout=timeout
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 559, in urlopen
_pool=self, _stacktrace=stacktrace)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/retry.py", line 245, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/packages/six.py", line 309, in reraise
raise value.with_traceback(tb)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 516, in urlopen
body=body, headers=headers)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 308, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 1086, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 924, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py", line 859, in send
self.connect()
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 154, in connect
conn = self._new_conn()
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 133, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 87, in create_connection
raise err
File "/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py", line 78, in create_connection
sock.connect(sa)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused'))

Issue 2:
Hi,

the validation of header type makes it impossible to use a str subclass.

code of validation:

requests/requests/utils.py

Line 1036
in
ec553c2

if type(part) not in HEADER_VALIDATORS:

I am rendering a the header by using the Django Template engine which returns a django.utils.safestring.SafeString. When using this as a header I get the

requests.exceptions.InvalidHeader: Header part ('application/xml') from {'accept': 'application/xml'} must be of type str or bytes, not <class 'django.utils.safestring.SafeString'>

exception.

May be using a construct with isinstance(ret, tuple(HEADER_VALIDATORS)):

could solve the problem.

thx & br

Matthias