summaryrefslogtreecommitdiffstats
path: root/Lib/urllib.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-06-22 19:00:13 (GMT)
committerGuido van Rossum <guido@python.org>1995-06-22 19:00:13 (GMT)
commit6cb15a0572b0a8ca32016e18bea5c7924303ee3b (patch)
tree7a3dba0d00e451d8cbcaf001bc3b192ddfeda445 /Lib/urllib.py
parent2ab19920fc0ba6a0054aa4556bef94199aa432fc (diff)
downloadcpython-6cb15a0572b0a8ca32016e18bea5c7924303ee3b.zip
cpython-6cb15a0572b0a8ca32016e18bea5c7924303ee3b.tar.gz
cpython-6cb15a0572b0a8ca32016e18bea5c7924303ee3b.tar.bz2
add User-agent hdr; read and close the file upon http error
Diffstat (limited to 'Lib/urllib.py')
-rw-r--r--Lib/urllib.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index c49f032..b7a1ce8 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -19,6 +19,9 @@ import socket
import regex
+__version__ = '1.0'
+
+
# This really consists of two pieces:
# (1) a class which handles opening of all sorts of URLs
# (plus assorted utilities etc.)
@@ -51,7 +54,8 @@ class URLopener:
# Constructor
def __init__(self):
- self.addheaders = []
+ server_version = "Python-urllib/%s" % __version__
+ self.addheaders = [('User-agent', server_version)]
self.tempcache = None
# Undocumented feature: if you assign {} to tempcache,
# it is used to cache files retrieved with
@@ -146,9 +150,15 @@ class URLopener:
h = httplib.HTTP(host)
h.putrequest('GET', selector)
for args in self.addheaders: apply(h.putheader, args)
+ h.endheaders()
errcode, errmsg, headers = h.getreply()
- if errcode == 200: return addinfo(h.getfile(), headers)
- else: raise IOError, ('http error', errcode, errmsg, headers)
+ fp = h.getfile()
+ if errcode == 200:
+ return addinfo(fp, headers)
+ else:
+ n = len(fp.read())
+ fp.close()
+ raise IOError, ('http error', errcode, errmsg, headers)
# Use Gopher protocol
def open_gopher(self, url):
@@ -322,6 +332,7 @@ class addbase:
self.readline = None
self.readlines = None
self.fileno = None
+ if self.fp: self.fp.close()
self.fp = None
# Class to add a close hook to an open file