11"""An extensible library for opening URLs using a variety of protocols
22
33The simplest way to use this module is to call the urlopen function,
4- which accepts a string containing a URL or a Request object (described
4+ which accepts a string containing a URL or a Request object (described
55below). It opens the URL and returns the results as file-like
66object; the returned object has some extra methods described below.
77
88The OpenerDirectory manages a collection of Handler objects that do
9- all the actual work. Each Handler implements a particular protocol or
9+ all the actual work. Each Handler implements a particular protocol or
1010option. The OpenerDirector is a composite object that invokes the
1111Handlers needed to open the requested URL. For example, the
1212HTTPHandler performs HTTP GET and POST requests and deals with
1616
1717urlopen(url, data=None) -- basic usage is that same as original
1818urllib. pass the url and optionally data to post to an HTTP URL, and
19- get a file-like object back. One difference is that you can also pass
19+ get a file-like object back. One difference is that you can also pass
2020a Request instance instead of URL. Raises a URLError (subclass of
2121IOError); for HTTP errors, raises an HTTPError, which can also be
2222treated as a valid response.
4242URLError-- a subclass of IOError, individual protocols have their own
4343specific subclass
4444
45- HTTPError-- also a valid HTTP response, so you can treat an HTTP error
45+ HTTPError-- also a valid HTTP response, so you can treat an HTTP error
4646as an exceptional event or valid response
4747
4848internals:
5757authinfo = urllib2.HTTPBasicAuthHandler()
5858authinfo.add_password('realm', 'host', 'username', 'password')
5959
60- # build a new opener that adds authentication and caching FTP handlers
60+ # build a new opener that adds authentication and caching FTP handlers
6161opener = urllib2.build_opener(authinfo, urllib2.CacheFTPHandler)
6262
6363# install it
7373 # authentication for some reason but fails, how should the error be
7474 # signalled? The client needs to know the HTTP error code. But if
7575 # the handler knows that the problem was, e.g., that it didn't know
76- # that hash algo that requested in the challenge, it would be good to
76+ # that hash algo that requested in the challenge, it would be good to
7777 # pass that information along to the client, too.
7878
7979# XXX to do:
@@ -141,7 +141,7 @@ def install_opener(opener):
141141 _opener = opener
142142
143143# do these error classes make sense?
144- # make sure all of the IOError stuff is overridden. we just want to be
144+ # make sure all of the IOError stuff is overridden. we just want to be
145145 # subtypes.
146146
147147class URLError (IOError ):
@@ -165,7 +165,7 @@ def __init__(self, url, code, msg, hdrs, fp):
165165 self .fp = fp
166166 # XXX
167167 self .filename = url
168-
168+
169169 def __str__ (self ):
170170 return 'HTTP Error %s: %s' % (self .code , self .msg )
171171
@@ -192,7 +192,7 @@ def __init__(self, url, data=None, headers={}):
192192
193193 def __getattr__ (self , attr ):
194194 # XXX this is a fallback mechanism to guard against these
195- # methods getting called in a non-standard order. this may be
195+ # methods getting called in a non-standard order. this may be
196196 # too complicated and/or unnecessary.
197197 # XXX should the __r_XXX attributes be public?
198198 if attr [:12 ] == '_Request__r_' :
@@ -259,7 +259,7 @@ def add_handler(self, handler):
259259 for meth in get_methods (handler ):
260260 if meth [- 5 :] == '_open' :
261261 protocol = meth [:- 5 ]
262- if self .handle_open .has_key (protocol ):
262+ if self .handle_open .has_key (protocol ):
263263 self .handle_open [protocol ].append (handler )
264264 else :
265265 self .handle_open [protocol ] = [handler ]
@@ -285,7 +285,7 @@ def add_handler(self, handler):
285285 if added :
286286 self .handlers .append (handler )
287287 handler .add_parent (self )
288-
288+
289289 def __del__ (self ):
290290 self .close ()
291291
@@ -314,9 +314,9 @@ def open(self, fullurl, data=None):
314314 if data is not None :
315315 req .add_data (data )
316316 assert isinstance (req , Request ) # really only care about interface
317-
317+
318318 result = self ._call_chain (self .handle_open , 'default' ,
319- 'default_open' , req )
319+ 'default_open' , req )
320320 if result :
321321 return result
322322
@@ -381,7 +381,7 @@ def get_methods(inst):
381381# XXX probably also want an abstract factory that knows things like
382382 # the fact that a ProxyHandler needs to get inserted first.
383383# would also know when it makes sense to skip a superclass in favor of
384- # a subclass and when it might make sense to include both
384+ # a subclass and when it might make sense to include both
385385
386386def build_opener (* handlers ):
387387 """Create an opener object from a list of handlers.
@@ -393,7 +393,7 @@ def build_opener(*handlers):
393393 If any of the handlers passed as arguments are subclasses of the
394394 default handlers, the default handlers will not be used.
395395 """
396-
396+
397397 opener = OpenerDirector ()
398398 default_classes = [ProxyHandler , UnknownHandler , HTTPHandler ,
399399 HTTPDefaultErrorHandler , HTTPRedirectHandler ,
@@ -472,7 +472,7 @@ def __init__(self, proxies=None):
472472 assert hasattr (proxies , 'has_key' ), "proxies must be a mapping"
473473 self .proxies = proxies
474474 for type , url in proxies .items ():
475- setattr (self , '%s_open' % type ,
475+ setattr (self , '%s_open' % type ,
476476 lambda r , proxy = url , type = type , meth = self .proxy_open : \
477477 meth (r , proxy , type ))
478478
@@ -574,7 +574,7 @@ def is_suburi(self, base, test):
574574 if len (common ) == len (base [1 ]):
575575 return 1
576576 return 0
577-
577+
578578
579579class HTTPBasicAuthHandler (BaseHandler ):
580580 rx = re .compile ('[ \t ]*([^ \t ]+)[ \t ]+realm="([^"]*)"' )
@@ -590,8 +590,8 @@ def __init__(self):
590590 # if __current_realm is not None, then the server must have
591591 # refused our name/password and is asking for authorization
592592 # again. must be careful to set it to None on successful
593- # return.
594-
593+ # return.
594+
595595 def http_error_401 (self , req , fp , code , msg , headers ):
596596 # XXX could be mult. headers
597597 authreq = headers .get ('www-authenticate' , None )
@@ -674,7 +674,7 @@ def get_authorization(self, req, chal):
674674 return None
675675
676676 user , pw = self .passwd .find_user_password (realm ,
677- req .get_full_url ())
677+ req .get_full_url ())
678678 if user is None :
679679 return None
680680
@@ -724,8 +724,8 @@ def encode_digest(digest):
724724 n = ord (c ) & 0xf
725725 hexrep .append (hex (n )[- 1 ])
726726 return string .join (hexrep , '' )
727-
728-
727+
728+
729729class HTTPHandler (BaseHandler ):
730730 def http_open (self , req ):
731731 # XXX devise a new mechanism to specify user/password
@@ -745,7 +745,7 @@ def http_open(self, req):
745745 h .putrequest ('GET' , req .get_selector ())
746746 except socket .error , err :
747747 raise URLError (err )
748-
748+
749749 # XXX proxies would have different host here
750750 h .putheader ('Host' , host )
751751 for args in self .parent .addheaders :
@@ -813,7 +813,7 @@ def parse_http_list(s):
813813 start = i
814814 inquote = 0
815815 else :
816- i = i + q
816+ i = i + q
817817 else :
818818 if c < q :
819819 list .append (s [start :i + c ])
@@ -838,7 +838,7 @@ def file_open(self, req):
838838 names = None
839839 def get_names (self ):
840840 if FileHandler .names is None :
841- FileHandler .names = (socket .gethostbyname ('localhost' ),
841+ FileHandler .names = (socket .gethostbyname ('localhost' ),
842842 socket .gethostbyname (socket .gethostname ()))
843843 return FileHandler .names
844844
@@ -967,7 +967,7 @@ def gopher_open(self, req):
967967class OpenerFactory :
968968
969969 default_handlers = [UnknownHandler , HTTPHandler ,
970- HTTPDefaultErrorHandler , HTTPRedirectHandler ,
970+ HTTPDefaultErrorHandler , HTTPRedirectHandler ,
971971 FTPHandler , FileHandler ]
972972 proxy_handlers = [ProxyHandler ]
973973 handlers = []
@@ -990,7 +990,7 @@ def build_opener(self):
990990 opener .add_handler (ph )
991991
992992if __name__ == "__main__" :
993- # XXX some of the test code depends on machine configurations that
993+ # XXX some of the test code depends on machine configurations that
994994 # are internal to CNRI. Need to set up a public server with the
995995 # right authentication configuration for test purposes.
996996 if socket .gethostname () == 'bitdiddle' :
@@ -1030,11 +1030,11 @@ def build_opener(self):
10301030
10311031 bauth = HTTPBasicAuthHandler ()
10321032 bauth .add_password ('basic_test_realm' , localhost , 'jhylton' ,
1033- 'password' )
1033+ 'password' )
10341034 dauth = HTTPDigestAuthHandler ()
1035- dauth .add_password ('digest_test_realm' , localhost , 'jhylton' ,
1035+ dauth .add_password ('digest_test_realm' , localhost , 'jhylton' ,
10361036 'password' )
1037-
1037+
10381038
10391039 cfh = CacheFTPHandler ()
10401040 cfh .setTimeout (1 )
0 commit comments