[New-bugs-announce] [issue8123] TypeError in urllib when trying to use HTTP authentication

Dmitry Jemerov report at bugs.python.org
Fri Mar 12 16:37:45 CET 2010


New submission from Dmitry Jemerov <intelliyole at gmail.com>:

I'm trying to download a file from a site using HTTP authentication. I'm subclassing FancyURLOpener, returning my credentials from the prompt_user_passwd() method, and using opener.retrieve() to download the file. I get the following error:

  File "C:/JetBrains/IDEA/build/eap/downandup.py", line 36, in download
    opener.retrieve(url, os.path.join(target_path, name))
  File "C:\Python31\lib\urllib\request.py", line 1467, in retrieve
    fp = self.open(url, data)
  File "C:\Python31\lib\urllib\request.py", line 1435, in open
    return getattr(self, name)(url)
  File "C:\Python31\lib\urllib\request.py", line 1609, in open_http
    return self._open_generic_http(http.client.HTTPConnection, url, data)
  File "C:\Python31\lib\urllib\request.py", line 1605, in _open_generic_http
    response.status, response.reason, response.msg, data)
  File "C:\Python31\lib\urllib\request.py", line 1621, in http_error
    result = method(url, fp, errcode, errmsg, headers)
  File "C:\Python31\lib\urllib\request.py", line 1859, in http_error_401
    return getattr(self,name)(url, realm)
  File "C:\Python31\lib\urllib\request.py", line 1931, in retry_http_basic_auth
    return self.open(newurl)
  File "C:\Python31\lib\urllib\request.py", line 1435, in open
    return getattr(self, name)(url)
  File "C:\Python31\lib\urllib\request.py", line 1609, in open_http
    return self._open_generic_http(http.client.HTTPConnection, url, data)
  File "C:\Python31\lib\urllib\request.py", line 1571, in _open_generic_http
    auth = base64.b64encode(user_passwd).strip()
  File "C:\Python31\lib\base64.py", line 56, in b64encode
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str

The problem happens because _open_generic_http extracts the user password from the string URL, and passes the string to the b64encode method, which only accepts bytes and not strings. The problem happens with Python 3.1.1 for me, but as far as I can see it's still not fixed in the py3k branch as of now.

----------
components: Library (Lib)
messages: 100938
nosy: Dmitry.Jemerov
severity: normal
status: open
title: TypeError in urllib when trying to use HTTP authentication
versions: Python 3.1

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


More information about the New-bugs-announce mailing list