Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
urllib2 Namespace Reference

Data Structures

class  URLError
 
class  HTTPError
 
class  GopherError
 
class  Request
 
class  OpenerDirector
 
class  BaseHandler
 
class  HTTPDefaultErrorHandler
 
class  HTTPRedirectHandler
 
class  ProxyHandler
 
class  CustomProxy
 
class  CustomProxyHandler
 
class  HTTPPasswordMgr
 
class  HTTPPasswordMgrWithDefaultRealm
 
class  AbstractBasicAuthHandler
 
class  HTTPBasicAuthHandler
 
class  ProxyBasicAuthHandler
 
class  AbstractDigestAuthHandler
 
class  HTTPDigestAuthHandler
 
class  ProxyDigestAuthHandler
 
class  AbstractHTTPHandler
 
class  HTTPHandler
 
class  HTTPSHandler
 
class  UnknownHandler
 
class  FileHandler
 
class  FTPHandler
 
class  CacheFTPHandler
 fw.ftp.set_debuglevel(1) More...
 
class  GopherHandler
 
class  OpenerFactory
 

Functions

def urlopen
 
def install_opener
 
def build_opener
 
def encode_digest
 
def parse_keqv_list
 
def parse_http_list
 

Variables

 sha = None
 
string __version__ = "2.0a1"
 
 _opener = None
 
string localhost = 'bitdiddle.cnri.reston.va.us'
 
list urls
 
tuple cfh = CacheFTPHandler()
 if localhost is not None: urls = urls + [ 'file://%s/etc/passwd' % localhost, 'http://%s/simple/' % localhost, 'http://%s/digest/' % localhost, 'http://%s/not/found.h' % localhost, ] More...
 
 req = None
 

XXX try out some custom proxy objects too!

def at_cnri(req): host = req.get_host() print host if host[-18:] == '.cnri.reston.va.us': return 1 p = CustomProxy('http', at_cnri, 'proxy.cnri.reston.va.us') ph = CustomProxyHandler(p) More...
 
tuple f = urlopen(url, req)
 
tuple buf = f.read()
 

Detailed Description

An extensible library for opening URLs using a variety of protocols

The simplest way to use this module is to call the urlopen function,
which accepts a string containing a URL or a Request object (described
below).  It opens the URL and returns the results as file-like
object; the returned object has some extra methods described below.

The OpenerDirectory manages a collection of Handler objects that do
all the actual work.  Each Handler implements a particular protocol or
option.  The OpenerDirector is a composite object that invokes the
Handlers needed to open the requested URL.  For example, the
HTTPHandler performs HTTP GET and POST requests and deals with
non-error returns.  The HTTPRedirectHandler automatically deals with
HTTP 301 & 302 redirect errors, and the HTTPDigestAuthHandler deals
with digest authentication.

urlopen(url, data=None) -- basic usage is that same as original
urllib.  pass the url and optionally data to post to an HTTP URL, and
get a file-like object back.  One difference is that you can also pass
a Request instance instead of URL.  Raises a URLError (subclass of
IOError); for HTTP errors, raises an HTTPError, which can also be
treated as a valid response.

build_opener -- function that creates a new OpenerDirector instance.
will install the default handlers.  accepts one or more Handlers as
arguments, either instances or Handler classes that it will
instantiate.  if one of the argument is a subclass of the default
handler, the argument will be installed instead of the default.

install_opener -- installs a new opener as the default opener.

objects of interest:
OpenerDirector --

Request -- an object that encapsulates the state of a request.  the
state can be a simple as the URL.  it can also include extra HTTP
headers, e.g. a User-Agent.

BaseHandler --

exceptions:
URLError-- a subclass of IOError, individual protocols have their own
specific subclass

HTTPError-- also a valid HTTP response, so you can treat an HTTP error
as an exceptional event or valid response

internals:
BaseHandler and parent
_call_chain conventions

Example usage:

import urllib2

# set up authentication info
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password('realm', 'host', 'username', 'password')

proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"})

# build a new opener that adds authentication and caching FTP handlers
opener = urllib2.build_opener(proxy_support, authinfo, urllib2.CacheFTPHandler)

# install it
urllib2.install_opener(opener)

f = urllib2.urlopen('http://www.python.org/')

Function Documentation

def urllib2.build_opener (   handlers)
Create an opener object from a list of handlers.

The opener will use several default handlers, including support
for HTTP and FTP.  If there is a ProxyHandler, it must be at the
front of the list of handlers.  (Yuck.)

If any of the handlers passed as arguments are subclasses of the
default handlers, the default handlers will not be used.

Definition at line 355 of file urllib2.py.

References inspect.isclass().

356 def build_opener(*handlers):
357  """Create an opener object from a list of handlers.
358 
359  The opener will use several default handlers, including support
360  for HTTP and FTP. If there is a ProxyHandler, it must be at the
361  front of the list of handlers. (Yuck.)
362 
363  If any of the handlers passed as arguments are subclasses of the
364  default handlers, the default handlers will not be used.
365  """
366 
367  opener = OpenerDirector()
368  default_classes = [ProxyHandler, UnknownHandler, HTTPHandler,
369  HTTPDefaultErrorHandler, HTTPRedirectHandler,
370  FTPHandler, FileHandler]
371  if hasattr(httplib, 'HTTPS'):
372  default_classes.append(HTTPSHandler)
373  skip = []
374  for klass in default_classes:
375  for check in handlers:
376  if inspect.isclass(check):
377  if issubclass(check, klass):
378  skip.append(klass)
379  elif isinstance(check, klass):
380  skip.append(klass)
381  for klass in skip:
382  default_classes.remove(klass)
383 
384  for klass in default_classes:
385  opener.add_handler(klass())
386 
387  for h in handlers:
388  if inspect.isclass(h):
389  h = h()
390  opener.add_handler(h)
391  return opener
def urllib2.encode_digest (   digest)

Definition at line 733 of file urllib2.py.

References dospath.join().

734 def encode_digest(digest):
735  hexrep = []
736  for c in digest:
737  n = (ord(c) >> 4) & 0xf
738  hexrep.append(hex(n)[-1])
739  n = ord(c) & 0xf
740  hexrep.append(hex(n)[-1])
741  return ''.join(hexrep)
742 
def urllib2.install_opener (   opener)

Definition at line 140 of file urllib2.py.

141 def install_opener(opener):
142  global _opener
143  _opener = opener
144 
145 # do these error classes make sense?
146 # make sure all of the IOError stuff is overridden. we just want to be
147  # subtypes.
def urllib2.parse_http_list (   s)
Parse lists as described by RFC 2068 Section 2.

In particular, parse comman-separated lists where the elements of
the list may include quoted-strings.  A quoted-string could
contain a comma.

Definition at line 810 of file urllib2.py.

811 def parse_http_list(s):
812  """Parse lists as described by RFC 2068 Section 2.
813 
814  In particular, parse comman-separated lists where the elements of
815  the list may include quoted-strings. A quoted-string could
816  contain a comma.
817  """
818  # XXX this function could probably use more testing
819 
820  list = []
821  end = len(s)
822  i = 0
823  inquote = 0
824  start = 0
825  while i < end:
826  cur = s[i:]
827  c = cur.find(',')
828  q = cur.find('"')
829  if c == -1:
830  list.append(s[start:])
831  break
832  if q == -1:
833  if inquote:
834  raise ValueError, "unbalanced quotes"
835  else:
836  list.append(s[start:i+c])
837  i = i + c + 1
838  continue
839  if inquote:
840  if q < c:
841  list.append(s[start:i+c])
842  i = i + c + 1
843  start = i
844  inquote = 0
845  else:
846  i = i + q
847  else:
848  if c < q:
849  list.append(s[start:i+c])
850  i = i + c + 1
851  start = i
852  else:
853  inquote = 1
854  i = i + q + 1
855  return map(lambda x: x.strip(), list)
def urllib2.parse_keqv_list (   l)
Parse list of key=value strings where keys are not duplicated.

Definition at line 800 of file urllib2.py.

801 def parse_keqv_list(l):
802  """Parse list of key=value strings where keys are not duplicated."""
803  parsed = {}
804  for elt in l:
805  k, v = elt.split('=', 1)
806  if v[0] == '"' and v[-1] == '"':
807  v = v[1:-1]
808  parsed[k] = v
809  return parsed
def urllib2.urlopen (   url,
  data = None 
)

Definition at line 134 of file urllib2.py.

References build_opener().

135 def urlopen(url, data=None):
136  global _opener
137  if _opener is None:
138  _opener = build_opener()
139  return _opener.open(url, data)

Variable Documentation

string __version__ = "2.0a1"

Definition at line 131 of file urllib2.py.

_opener = None

Definition at line 133 of file urllib2.py.

tuple buf = f.read()

Definition at line 1101 of file urllib2.py.

tuple cfh = CacheFTPHandler()

if localhost is not None: urls = urls + [ 'file://%s/etc/passwd' % localhost, 'http://%s/simple/' % localhost, 'http://%s/digest/' % localhost, 'http://%s/not/found.h' % localhost, ]

bauth = HTTPBasicAuthHandler() bauth.add_password('basic_test_realm', localhost, 'jhylton', 'password') dauth = HTTPDigestAuthHandler() dauth.add_password('digest_test_realm', localhost, 'jhylton', 'password')

Definition at line 1073 of file urllib2.py.

tuple f = urlopen(url, req)

Definition at line 1095 of file urllib2.py.

string localhost = 'bitdiddle.cnri.reston.va.us'

Definition at line 1035 of file urllib2.py.

req = None

XXX try out some custom proxy objects too!

def at_cnri(req): host = req.get_host() print host if host[-18:] == '.cnri.reston.va.us': return 1 p = CustomProxy('http', at_cnri, 'proxy.cnri.reston.va.us') ph = CustomProxyHandler(p)

install_opener(build_opener(dauth, bauth, cfh, GopherHandler, ph))

Definition at line 1092 of file urllib2.py.

sha = None

Definition at line 118 of file urllib2.py.

list urls
Initial value:
1 = [
2  # Thanks to Fred for finding these!
3  'gopher://gopher.lib.ncsu.edu/11/library/stacks/Alex',
4  'gopher://gopher.vt.edu:10010/10/33',
5 
6  'file:/etc/passwd',
7  'file://nonsensename/etc/passwd',
8  'ftp://www.python.org/pub/python/misc/sousa.au',
9  'ftp://www.python.org/pub/tmp/blat',
10  'http://www.espn.com/', # redirect
11  'http://www.python.org/Spanish/Inquistion/',
12  ('http://www.python.org/cgi-bin/faqw.py',
13  'query=pythonistas&querytype=simple&casefold=yes&req=search'),
14  'http://www.python.org/',
15  'ftp://gatekeeper.research.compaq.com/pub/DEC/SRC/research-reports/00README-Legal-Rules-Regs',
16  ]

Definition at line 1040 of file urllib2.py.