<p dir="auto">On the topic of requests not mapping exceptions to its own exceptions...</p>
<p dir="auto"><code class="notranslate">requests.packages.urllib3.exceptions.ProtocolError</code> is not caught. Reproduction: invalid status line.</p>
<p dir="auto">Server:<br>
<code class="notranslate">$ echo | nc -l 8000</code></p>
<p dir="auto">requests:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import requests
try:
    requests.get('http://localhost:8000')
except Exception as e:
    print (e.__class__)"><pre class="notranslate"><code class="notranslate">import requests
try:
    requests.get('http://localhost:8000')
except Exception as e:
    print (e.__class__)
</code></pre></div>
<p dir="auto">Output: &lt;class 'requests.packages.urllib3.exceptions.ProtocolError'&gt;</p>
<p dir="auto">I would expect this to be mapped to a requests exception.</p>
<p dir="auto">Connection refused comes out also as ProtocolError.</p>
<p dir="auto">This is requests 2.4.0.</p>
<p dir="auto">Ping <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/Lukasa/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/Lukasa">@Lukasa</a> , <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/sigmavirus24/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/sigmavirus24">@sigmavirus24</a></p>
<p dir="auto">Also happens with connection refused. This should be covered by requests unittests in the future.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
Traceback (most recent call last):
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 516, in urlopen
    body=body, headers=headers)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 308, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 1090, in request
    self._send_request(method, url, body, headers)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 1128, in _send_request
    self.endheaders(body)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 1086, in endheaders
    self._send_output(message_body)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 924, in _send_output
    self.send(msg)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 859, in send
    self.connect()
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py&quot;, line 154, in connect
    conn = self._new_conn()
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py&quot;, line 133, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py&quot;, line 87, in create_connection
    raise err
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py&quot;, 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 &quot;&lt;ipython-input-14-1e96a12a07a3&gt;&quot;, line 1, in &lt;module&gt;
    try: requests.get('http://localhost:8000')
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/api.py&quot;, line 55, in get
    return request('get', url, **kwargs)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/api.py&quot;, line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/sessions.py&quot;, line 448, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/sessions.py&quot;, line 554, in send
    r = adapter.send(request, **kwargs)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/adapters.py&quot;, line 359, in send
    timeout=timeout
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 559, in urlopen
    _pool=self, _stacktrace=stacktrace)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/retry.py&quot;, line 245, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/packages/six.py&quot;, line 309, in reraise
    raise value.with_traceback(tb)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 516, in urlopen
    body=body, headers=headers)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py&quot;, line 308, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 1090, in request
    self._send_request(method, url, body, headers)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 1128, in _send_request
    self.endheaders(body)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 1086, in endheaders
    self._send_output(message_body)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 924, in _send_output
    self.send(msg)
  File &quot;/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/client.py&quot;, line 859, in send
    self.connect()
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py&quot;, line 154, in connect
    conn = self._new_conn()
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/connection.py&quot;, line 133, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py&quot;, line 87, in create_connection
    raise err
  File &quot;/Users/joneskoo/git/requests/ve/lib/python3.4/site-packages/requests/packages/urllib3/util/connection.py&quot;, line 78, in create_connection
    sock.connect(sa)
requests.packages.urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused'))"><pre class="notranslate"><code class="notranslate">
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 "&lt;ipython-input-14-1e96a12a07a3&gt;", line 1, in &lt;module&gt;
    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'))
</code></pre></div>
<p dir="auto">Hi,<br>
the validation of header type makes it impossible to use a str subclass.</p>
<p dir="auto">code of validation:<br>
</p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/psf/requests/blob/ec553c275fe915ab8c179a530907b564892d2922/requests/utils.py#L1036">requests/requests/utils.py</a>
    </p>
    <p class="mb-0 color-fg-muted">
         Line 1036
      in
      <a data-pjax="true" class="commit-tease-sha" href="/psf/requests/commit/ec553c275fe915ab8c179a530907b564892d2922">ec553c2</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L1036" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="1036"></td>
          <td id="LC1036" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">if</span> <span class="pl-en">type</span>(<span class="pl-s1">part</span>) <span class="pl-c1">not</span> <span class="pl-c1">in</span> <span class="pl-v">HEADER_VALIDATORS</span>: </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>
<p dir="auto">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</p>
<p dir="auto">requests.exceptions.InvalidHeader: Header part ('application/xml') from {'accept': 'application/xml'} must be of type str or bytes, not &lt;class 'django.utils.safestring.SafeString'&gt;<br>
exception.</p>
<p dir="auto">May be using a construct with isinstance(ret, tuple(HEADER_VALIDATORS)):<br>
could solve the problem.</p>
<p dir="auto">thx &amp; br<br>
Matthias</p>