[issue29891] urllib.request.Request accepts but doesn't check bytes headers

Martin Panter report at bugs.python.org
Fri Mar 24 21:01:46 EDT 2017


Martin Panter added the comment:

If you enable BytesWarning (python -b) you do get an error:

>>> urlopen(req, data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/urllib/request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 463, in open
    req = meth(req)
  File "/usr/lib/python3.5/urllib/request.py", line 1171, in do_request_
    if not request.has_header('Content-type'):
  File "/usr/lib/python3.5/urllib/request.py", line 356, in has_header
    return (header_name in self.headers or
BytesWarning: Comparison between bytes and string

I believe the “urllib.request” module is only written with text (str) field names in mind, not byte strings. Same for http.client.HTTPConnection.request(headers=...). But the lower-level HTTPConnection.putheader method has special code to handle byte strings: <http://svn.python.org/view?view=revision&revision=58823>, although this is not documented either.

----------
nosy: +martin.panter

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29891>
_______________________________________


More information about the Python-bugs-list mailing list