FancyURLOpener在升级到Python 3.1.2后失败
我有一个应用程序,它从一个需要密码的网站下载一个.CSV文件,然后进一步处理这个文件。
我使用了FancyURLOpener,并且直接把用户名和密码写死在代码里。(显然,在这种情况下,安全性不是首要考虑的问题)。
自从我下载了Python 3.1.2之后,这段代码就不再工作了。在解决了明显的问题(现在它在“request”命名空间中)后,程序又出现了不太明显的崩溃。
有没有人知道实现上发生了什么变化,以及现在该怎么使用?文档里似乎缺少示例。
这是代码的简化版本:
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;
为了完整起见,这里是整个调用栈:
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