diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/lib-tk/Tkinter.py | 2 | ||||
-rw-r--r-- | Lib/test/test_urllib.py | 15 | ||||
-rw-r--r-- | Lib/urllib.py | 73 |
3 files changed, 87 insertions, 3 deletions
diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py index aecb317..33b5ed0 100644 --- a/Lib/lib-tk/Tkinter.py +++ b/Lib/lib-tk/Tkinter.py @@ -2977,7 +2977,7 @@ class Text(Widget): return self.tk.call(self._w, "image", "names") def index(self, index): """Return the index in the form line.char for INDEX.""" - return self.tk.call(self._w, 'index', index) + return str(self.tk.call(self._w, 'index', index)) def insert(self, index, chars, *args): """Insert CHARS before the characters at INDEX. An additional tag can be given in ARGS. Additional CHARS and tags can follow in ARGS.""" diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 875903e..a87ab71 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -126,10 +126,23 @@ class urlopen_HttpTests(unittest.TestCase): finally: self.unfakehttp() + def test_read_bogus(self): + # urlopen() should raise IOError for many error codes. + self.fakehttp(b'''HTTP/1.1 401 Authentication Required +Date: Wed, 02 Jan 2008 03:03:54 GMT +Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e +Connection: close +Content-Type: text/html; charset=iso-8859-1 +''') + try: + self.assertRaises(IOError, urllib.urlopen, "http://python.org/") + finally: + self.unfakehttp() + def test_empty_socket(self): # urlopen() raises IOError if the underlying socket does not send any # data. (#1680230) - self.fakehttp(b"") + self.fakehttp(b'') try: self.assertRaises(IOError, urllib.urlopen, "http://something") finally: diff --git a/Lib/urllib.py b/Lib/urllib.py index 81a8cd6..df21419 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -359,7 +359,7 @@ class URLopener: # According to RFC 2616, "2xx" code indicates that the client's # request was successfully received, understood, and accepted. - if not (200 <= response.status < 300): + if (200 <= response.status < 300): return addinfourl(response.fp, response.msg, "http:" + url) else: return self.http_error( @@ -402,6 +402,77 @@ class URLopener: """Use HTTPS protocol.""" return self._open_generic_http(self._https_connection, url, data) + import httplib + user_passwd = None + proxy_passwd = None + if isinstance(url, str): + host, selector = splithost(url) + if host: + user_passwd, host = splituser(host) + host = unquote(host) + realhost = host + else: + host, selector = url + # here, we determine, whether the proxy contains authorization information + proxy_passwd, host = splituser(host) + urltype, rest = splittype(selector) + url = rest + user_passwd = None + if urltype.lower() != 'https': + realhost = None + else: + realhost, rest = splithost(rest) + if realhost: + user_passwd, realhost = splituser(realhost) + if user_passwd: + selector = "%s://%s%s" % (urltype, realhost, rest) + #print "proxy via https:", host, selector + if not host: raise IOError('https error', 'no host given') + if proxy_passwd: + import base64 + proxy_auth = base64.b64encode(proxy_passwd).strip() + else: + proxy_auth = None + if user_passwd: + import base64 + auth = base64.b64encode(user_passwd).strip() + else: + auth = None + h = httplib.HTTPS(host, 0, + key_file=self.key_file, + cert_file=self.cert_file) + if data is not None: + h.putrequest('POST', selector) + h.putheader('Content-Type', + 'application/x-www-form-urlencoded') + h.putheader('Content-Length', '%d' % len(data)) + else: + h.putrequest('GET', selector) + if proxy_auth: h.putheader('Proxy-Authorization', 'Basic %s' % proxy_auth) + if auth: h.putheader('Authorization', 'Basic %s' % auth) + if realhost: h.putheader('Host', realhost) + for args in self.addheaders: h.putheader(*args) + h.endheaders() + if data is not None: + h.send(data) + errcode, errmsg, headers = h.getreply() + fp = h.getfile() + if errcode == -1: + if fp: fp.close() + # something went wrong with the HTTP status line + raise IOError('http protocol error', 0, + 'got a bad status line', None) + # According to RFC 2616, "2xx" code indicates that the client's + # request was successfully received, understood, and accepted. + if (200 <= errcode < 300): + return addinfourl(fp, headers, "https:" + url) + else: + if data is None: + return self.http_error(url, fp, errcode, errmsg, headers) + else: + return self.http_error(url, fp, errcode, errmsg, headers, + data) + def open_file(self, url): """Use local file or FTP depending on form of URL.""" if not isinstance(url, str): |