Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
urllib.request.OpenerDirector Class Reference

List of all members.

Public Member Functions

def __init__
def add_handler
def close
def open
def error

Public Attributes

 addheaders
 handlers
 handle_open
 handle_error
 process_response
 process_request

Private Member Functions

def _call_chain
def _open

Detailed Description

Definition at line 277 of file request.py.


Constructor & Destructor Documentation

Definition at line 278 of file request.py.

00278 
00279     def __init__(self):
00280         client_version = "Python-urllib/%s" % __version__
00281         self.addheaders = [('User-agent', client_version)]
00282         # self.handlers is retained only for backward compatibility
00283         self.handlers = []
00284         # manage the individual handlers
00285         self.handle_open = {}
00286         self.handle_error = {}
00287         self.process_response = {}
00288         self.process_request = {}

Here is the caller graph for this function:


Member Function Documentation

def urllib.request.OpenerDirector._call_chain (   self,
  chain,
  kind,
  meth_name,
  args 
) [private]

Definition at line 340 of file request.py.

00340 
00341     def _call_chain(self, chain, kind, meth_name, *args):
00342         # Handlers raise an exception if no one else should try to handle
00343         # the request, or return None if they can't but another handler
00344         # could.  Otherwise, they return the response.
00345         handlers = chain.get(kind, ())
00346         for handler in handlers:
00347             func = getattr(handler, meth_name)
00348             result = func(*args)
00349             if result is not None:
00350                 return result

Here is the call graph for this function:

Here is the caller graph for this function:

def urllib.request.OpenerDirector._open (   self,
  req,
  data = None 
) [private]

Definition at line 379 of file request.py.

00379 
00380     def _open(self, req, data=None):
00381         result = self._call_chain(self.handle_open, 'default',
00382                                   'default_open', req)
00383         if result:
00384             return result
00385 
00386         protocol = req.type
00387         result = self._call_chain(self.handle_open, protocol, protocol +
00388                                   '_open', req)
00389         if result:
00390             return result
00391 
00392         return self._call_chain(self.handle_open, 'unknown',
00393                                 'unknown_open', req)

Here is the call graph for this function:

Here is the caller graph for this function:

def urllib.request.OpenerDirector.add_handler (   self,
  handler 
)

Definition at line 289 of file request.py.

00289 
00290     def add_handler(self, handler):
00291         if not hasattr(handler, "add_parent"):
00292             raise TypeError("expected BaseHandler instance, got %r" %
00293                             type(handler))
00294 
00295         added = False
00296         for meth in dir(handler):
00297             if meth in ["redirect_request", "do_open", "proxy_open"]:
00298                 # oops, coincidental match
00299                 continue
00300 
00301             i = meth.find("_")
00302             protocol = meth[:i]
00303             condition = meth[i+1:]
00304 
00305             if condition.startswith("error"):
00306                 j = condition.find("_") + i + 1
00307                 kind = meth[j+1:]
00308                 try:
00309                     kind = int(kind)
00310                 except ValueError:
00311                     pass
00312                 lookup = self.handle_error.get(protocol, {})
00313                 self.handle_error[protocol] = lookup
00314             elif condition == "open":
00315                 kind = protocol
00316                 lookup = self.handle_open
00317             elif condition == "response":
00318                 kind = protocol
00319                 lookup = self.process_response
00320             elif condition == "request":
00321                 kind = protocol
00322                 lookup = self.process_request
00323             else:
00324                 continue
00325 
00326             handlers = lookup.setdefault(kind, [])
00327             if handlers:
00328                 bisect.insort(handlers, handler)
00329             else:
00330                 handlers.append(handler)
00331             added = True
00332 
00333         if added:
00334             bisect.insort(self.handlers, handler)
00335             handler.add_parent(self)

Definition at line 336 of file request.py.

00336 
00337     def close(self):
00338         # Only exists for backwards compatibility.
00339         pass

Here is the caller graph for this function:

def urllib.request.OpenerDirector.error (   self,
  proto,
  args 
)

Definition at line 394 of file request.py.

00394 
00395     def error(self, proto, *args):
00396         if proto in ('http', 'https'):
00397             # XXX http[s] protocols are special-cased
00398             dict = self.handle_error['http'] # https is not different than http
00399             proto = args[2]  # YUCK!
00400             meth_name = 'http_error_%s' % proto
00401             http_err = 1
00402             orig_args = args
00403         else:
00404             dict = self.handle_error
00405             meth_name = proto + '_error'
00406             http_err = 0
00407         args = (dict, proto, meth_name) + args
00408         result = self._call_chain(*args)
00409         if result:
00410             return result
00411 
00412         if http_err:
00413             args = (dict, 'default', 'http_error_default') + orig_args
00414             return self._call_chain(*args)
00415 
00416 # XXX probably also want an abstract factory that knows when it makes
00417 # sense to skip a superclass in favor of a subclass and when it might
00418 # make sense to include both

Here is the call graph for this function:

Here is the caller graph for this function:

def urllib.request.OpenerDirector.open (   self,
  fullurl,
  data = None,
  timeout = socket._GLOBAL_DEFAULT_TIMEOUT 
)

Definition at line 351 of file request.py.

00351 
00352     def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
00353         # accept a URL or a Request object
00354         if isinstance(fullurl, str):
00355             req = Request(fullurl, data)
00356         else:
00357             req = fullurl
00358             if data is not None:
00359                 req.data = data
00360 
00361         req.timeout = timeout
00362         protocol = req.type
00363 
00364         # pre-process request
00365         meth_name = protocol+"_request"
00366         for processor in self.process_request.get(protocol, []):
00367             meth = getattr(processor, meth_name)
00368             req = meth(req)
00369 
00370         response = self._open(req, data)
00371 
00372         # post-process response
00373         meth_name = protocol+"_response"
00374         for processor in self.process_response.get(protocol, []):
00375             meth = getattr(processor, meth_name)
00376             response = meth(req, response)
00377 
00378         return response

Here is the call graph for this function:


Member Data Documentation

Definition at line 280 of file request.py.

Definition at line 285 of file request.py.

Definition at line 284 of file request.py.

Definition at line 282 of file request.py.

Definition at line 287 of file request.py.

Definition at line 286 of file request.py.


The documentation for this class was generated from the following file: