mirror of https://github.com/python/cpython.git
add User-agent hdr; read and close the file upon http error
This commit is contained in:
parent
2ab19920fc
commit
6cb15a0572
|
@ -19,6 +19,9 @@
|
||||||
import regex
|
import regex
|
||||||
|
|
||||||
|
|
||||||
|
__version__ = '1.0'
|
||||||
|
|
||||||
|
|
||||||
# This really consists of two pieces:
|
# This really consists of two pieces:
|
||||||
# (1) a class which handles opening of all sorts of URLs
|
# (1) a class which handles opening of all sorts of URLs
|
||||||
# (plus assorted utilities etc.)
|
# (plus assorted utilities etc.)
|
||||||
|
@ -51,7 +54,8 @@ class URLopener:
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.addheaders = []
|
server_version = "Python-urllib/%s" % __version__
|
||||||
|
self.addheaders = [('User-agent', server_version)]
|
||||||
self.tempcache = None
|
self.tempcache = None
|
||||||
# Undocumented feature: if you assign {} to tempcache,
|
# Undocumented feature: if you assign {} to tempcache,
|
||||||
# it is used to cache files retrieved with
|
# it is used to cache files retrieved with
|
||||||
|
@ -146,9 +150,15 @@ def open_http(self, url):
|
||||||
h = httplib.HTTP(host)
|
h = httplib.HTTP(host)
|
||||||
h.putrequest('GET', selector)
|
h.putrequest('GET', selector)
|
||||||
for args in self.addheaders: apply(h.putheader, args)
|
for args in self.addheaders: apply(h.putheader, args)
|
||||||
|
h.endheaders()
|
||||||
errcode, errmsg, headers = h.getreply()
|
errcode, errmsg, headers = h.getreply()
|
||||||
if errcode == 200: return addinfo(h.getfile(), headers)
|
fp = h.getfile()
|
||||||
else: raise IOError, ('http error', errcode, errmsg, headers)
|
if errcode == 200:
|
||||||
|
return addinfo(fp, headers)
|
||||||
|
else:
|
||||||
|
n = len(fp.read())
|
||||||
|
fp.close()
|
||||||
|
raise IOError, ('http error', errcode, errmsg, headers)
|
||||||
|
|
||||||
# Use Gopher protocol
|
# Use Gopher protocol
|
||||||
def open_gopher(self, url):
|
def open_gopher(self, url):
|
||||||
|
@ -322,6 +332,7 @@ def close(self):
|
||||||
self.readline = None
|
self.readline = None
|
||||||
self.readlines = None
|
self.readlines = None
|
||||||
self.fileno = None
|
self.fileno = None
|
||||||
|
if self.fp: self.fp.close()
|
||||||
self.fp = None
|
self.fp = None
|
||||||
|
|
||||||
# Class to add a close hook to an open file
|
# Class to add a close hook to an open file
|
||||||
|
|
Loading…
Reference in New Issue