1

I had an application that was downloading a .CSV file from a password-protected website then processing it futher.

I was using FancyURLOpener, and simply hardcoding the username and password. (Obviously, security is not a high priority in this particular instance).

Since downloading Python 3.1.2, this code has stopped working. After fixing the obvious issue of it now being in the "request" namespace, it's crashing in a less obvious way.

Does anyone know of the changes that have happened to the implementation, and how to use it now? The documentation seems to be short of examples.

Here is a cut down version of the code:

import urllib.request;

class TracOpener (urllib.request.FancyURLopener) :
    def prompt_user_passwd(self, host, realm) :
        return ('andrew_ee', '_my_unenctryped_password')



csvUrl='http://mysite/report/19?format=csv@USER=fred_nukre'

opener = TracOpener();
f = opener.open(csvUrl);  # This is failing!
s = f.read();
f.close();
s;

For the sake of completeness, here's the entire call stack:

Traceback (most recent call last):
  File "C:\reporting\download_csv_file.py", line 12, in <module>
    f = opener.open(csvUrl);
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1454, in open
    return getattr(self, name)(url)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1628, in open_http
    return self._open_generic_http(http.client.HTTPConnection, url, data)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1624, in _open_generic_http
    response.status, response.reason, response.msg, data)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1640, in http_error
    result = method(url, fp, errcode, errmsg, headers)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1878, in http_error_401
    return getattr(self,name)(url, realm)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1950, in retry_http_basic_auth
    return self.open(newurl)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1454, in open
    return getattr(self, name)(url)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1628, in open_http
    return self._open_generic_http(http.client.HTTPConnection, url, data)
  File "C:\Program Files\Python31\lib\urllib\request.py", line 1590, in _open_generic_http
    auth = base64.b64encode(user_passwd).strip()
  File "C:\Program Files\Python31\lib\base64.py", line 56, in b64encode
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205

1 Answers1

1

It's a known bug: http://bugs.python.org/issue8123

Ned Deily
  • 83,389
  • 16
  • 128
  • 151