FancyURLOpener failing since moving to python 3.1.2

Posted by Andrew Shepherd on Stack Overflow See other posts from Stack Overflow or by Andrew Shepherd
Published on 2010-03-25T01:10:06Z Indexed on 2010/03/25 1:13 UTC
Read the original article Hit count: 485

Filed under:

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. Does anyone know of the changes that have happened to the implementation?

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);
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

© Stack Overflow or respective owner

Related posts about python-3.x