8989
9090import socket
9191import httplib
92+ import inspect
9293import re
9394import base64
9495import types
@@ -252,7 +253,7 @@ def __init__(self):
252253
253254 def add_handler (self , handler ):
254255 added = 0
255- for meth in get_methods (handler ):
256+ for meth in dir (handler ):
256257 if meth [- 5 :] == '_open' :
257258 protocol = meth [:- 5 ]
258259 if self .handle_open .has_key (protocol ):
@@ -303,7 +304,7 @@ def _call_chain(self, chain, kind, meth_name, *args):
303304
304305 def open (self , fullurl , data = None ):
305306 # accept a URL or a Request object
306- if isinstance (fullurl , types .StringType ):
307+ if isinstance (fullurl , ( types .StringType , types . UnicodeType ) ):
307308 req = Request (fullurl , data )
308309 else :
309310 req = fullurl
@@ -346,34 +347,6 @@ def error(self, proto, *args):
346347 args = (dict , 'default' , 'http_error_default' ) + orig_args
347348 return self ._call_chain (* args )
348349
349- def is_callable (obj ):
350- # not quite like builtin callable (which I didn't know existed),
351- # not entirely sure it needs to be different
352- if type (obj ) in (types .BuiltinFunctionType ,
353- types .BuiltinMethodType , types .LambdaType ,
354- types .MethodType ):
355- return 1
356- if isinstance (obj , types .InstanceType ):
357- return hasattr (obj , '__call__' )
358- return 0
359-
360- def get_methods (inst ):
361- methods = {}
362- classes = []
363- classes .append (inst .__class__ )
364- while classes :
365- klass = classes [0 ]
366- del classes [0 ]
367- classes = classes + list (klass .__bases__ )
368- for name in dir (klass ):
369- attr = getattr (klass , name )
370- if isinstance (attr , types .UnboundMethodType ):
371- methods [name ] = 1
372- for name in dir (inst ):
373- if is_callable (getattr (inst , name )):
374- methods [name ] = 1
375- return methods .keys ()
376-
377350# XXX probably also want an abstract factory that knows things like
378351 # the fact that a ProxyHandler needs to get inserted first.
379352# would also know when it makes sense to skip a superclass in favor of
@@ -399,20 +372,19 @@ def build_opener(*handlers):
399372 skip = []
400373 for klass in default_classes :
401374 for check in handlers :
402- if isinstance (check , types . ClassType ):
375+ if inspect . isclass (check ):
403376 if issubclass (check , klass ):
404377 skip .append (klass )
405- elif isinstance (check , types .InstanceType ):
406- if isinstance (check , klass ):
407- skip .append (klass )
378+ elif isinstance (check , klass ):
379+ skip .append (klass )
408380 for klass in skip :
409381 default_classes .remove (klass )
410382
411383 for klass in default_classes :
412384 opener .add_handler (klass ())
413385
414386 for h in handlers :
415- if isinstance ( h , types . ClassType ):
387+ if inspect . isclass ( h ):
416388 h = h ()
417389 opener .add_handler (h )
418390 return opener
@@ -545,7 +517,7 @@ def __init__(self):
545517
546518 def add_password (self , realm , uri , user , passwd ):
547519 # uri could be a single URI or a sequence
548- if isinstance (uri , types .StringType ):
520+ if isinstance (uri , ( types .StringType , types . UnicodeType ) ):
549521 uri = [uri ]
550522 uri = tuple (map (self .reduce_uri , uri ))
551523 if not self .passwd .has_key (realm ):
@@ -1067,7 +1039,7 @@ def replace_handler(self, h):
10671039 def build_opener (self ):
10681040 opener = OpenerDirector ()
10691041 for ph in self .proxy_handlers :
1070- if isinstance (ph , types . ClassType ):
1042+ if inspect . isclass (ph ):
10711043 ph = ph ()
10721044 opener .add_handler (ph )
10731045
@@ -1088,49 +1060,46 @@ def build_opener(self):
10881060
10891061 'file:/etc/passwd' ,
10901062 'file://nonsensename/etc/passwd' ,
1091- 'ftp://www.python.org/pub/tmp/httplib.py' ,
1092- 'ftp://www.python.org/pub/tmp/imageop.c' ,
1063+ 'ftp://www.python.org/pub/python/misc/sousa.au' ,
10931064 'ftp://www.python.org/pub/tmp/blat' ,
10941065 'http://www.espn.com/' , # redirect
10951066 'http://www.python.org/Spanish/Inquistion/' ,
1096- ('http://grail.cnri.reston.va.us /cgi-bin/faqw.py' ,
1067+ ('http://www.python.org /cgi-bin/faqw.py' ,
10971068 'query=pythonistas&querytype=simple&casefold=yes&req=search' ),
10981069 'http://www.python.org/' ,
1099- 'ftp://prep.ai.mit.edu/welcome.msg' ,
1100- 'ftp://www.python.org/pub/tmp/figure.prn' ,
1101- 'ftp://www.python.org/pub/tmp/interp.pl' ,
1102- 'http://checkproxy.cnri.reston.va.us/test/test.html' ,
1070+ 'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/00README-Legal-Rules-Regs' ,
11031071 ]
11041072
1105- if localhost is not None :
1106- urls = urls + [
1107- 'file://%s/etc/passwd' % localhost ,
1108- 'http://%s/simple/' % localhost ,
1109- 'http://%s/digest/' % localhost ,
1110- 'http://%s/not/found.h' % localhost ,
1111- ]
1073+ ## if localhost is not None:
1074+ ## urls = urls + [
1075+ ## 'file://%s/etc/passwd' % localhost,
1076+ ## 'http://%s/simple/' % localhost,
1077+ ## 'http://%s/digest/' % localhost,
1078+ ## 'http://%s/not/found.h' % localhost,
1079+ ## ]
11121080
1113- bauth = HTTPBasicAuthHandler ()
1114- bauth .add_password ('basic_test_realm' , localhost , 'jhylton' ,
1115- 'password' )
1116- dauth = HTTPDigestAuthHandler ()
1117- dauth .add_password ('digest_test_realm' , localhost , 'jhylton' ,
1118- 'password' )
1081+ ## bauth = HTTPBasicAuthHandler()
1082+ ## bauth.add_password('basic_test_realm', localhost, 'jhylton',
1083+ ## 'password')
1084+ ## dauth = HTTPDigestAuthHandler()
1085+ ## dauth.add_password('digest_test_realm', localhost, 'jhylton',
1086+ ## 'password')
11191087
11201088
11211089 cfh = CacheFTPHandler ()
11221090 cfh .setTimeout (1 )
11231091
1124- # XXX try out some custom proxy objects too!
1125- def at_cnri (req ):
1126- host = req .get_host ()
1127- print host
1128- if host [- 18 :] == '.cnri.reston.va.us' :
1129- return 1
1130- p = CustomProxy ('http' , at_cnri , 'proxy.cnri.reston.va.us' )
1131- ph = CustomProxyHandler (p )
1132-
1133- #install_opener(build_opener(dauth, bauth, cfh, GopherHandler, ph))
1092+ ## # XXX try out some custom proxy objects too!
1093+ ## def at_cnri(req):
1094+ ## host = req.get_host()
1095+ ## print host
1096+ ## if host[-18:] == '.cnri.reston.va.us':
1097+ ## return 1
1098+ ## p = CustomProxy('http', at_cnri, 'proxy.cnri.reston.va.us')
1099+ ## ph = CustomProxyHandler(p)
1100+
1101+ ## install_opener(build_opener(dauth, bauth, cfh, GopherHandler, ph))
1102+ install_opener (build_opener (cfh , GopherHandler ))
11341103
11351104 for url in urls :
11361105 if isinstance (url , types .TupleType ):
0 commit comments