home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / common / asynchttp / requester.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  6.2 KB  |  179 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import atexit
  5. import logging
  6. import httplib
  7. import urllib2
  8. import util
  9. _log = log = logging.getLogger('asynchttp')
  10. import connection
  11. import httptypes
  12. __all__ = [
  13.     'HttpMaster',
  14.     'httpopen']
  15.  
  16. class HttpMaster(object):
  17.     persister_cls = connection.HttpPersister
  18.     request_cls = httptypes.HTTPRequest
  19.     
  20.     def key(cls, thing):
  21.         if isinstance(thing, (cls.persister_cls, cls.persister_cls.connection_cls)):
  22.             return '%s:%s' % (thing.host, thing.port)
  23.         if isinstance(thing, cls.request_cls):
  24.             host = thing.get_host()
  25.             o_r_host = thing.get_origin_req_host()
  26.             if o_r_host in host:
  27.                 if o_r_host != host:
  28.                     pass
  29.                 else:
  30.                     host = o_r_host
  31.             
  32.             url = thing.get_full_url()
  33.             if ':' in host:
  34.                 (host, port) = host.split(':')
  35.             elif url.startswith('https'):
  36.                 port = httplib.HTTPS_PORT
  37.             else:
  38.                 port = httplib.HTTP_PORT
  39.             return '%s:%s' % (host, port)
  40.         raise TypeError("Don't know how to key this object: %r", thing)
  41.  
  42.     key = classmethod(key)
  43.     
  44.     def host_port(cls, forkey):
  45.         return forkey.split(':')[:2]
  46.  
  47.     host_port = classmethod(host_port)
  48.     
  49.     def __init__(self):
  50.         self._conns = { }
  51.  
  52.     
  53.     def __repr__(self):
  54.         return '<%s with %r active connections (id=0x%x)>' % (type(self).__name__, len(self._conns), id(self))
  55.  
  56.     
  57.     def request(self, full_url, data = None, *a, **k):
  58.         cb = k.pop('callback')
  59.         if isinstance(full_url, basestring):
  60.             req = self.request_cls.make_request(full_url, data, *a, **k)
  61.         else:
  62.             req = full_url
  63.         self._do_request(req, cb)
  64.  
  65.     request = util.callsback(request)
  66.     open = request
  67.     
  68.     def _do_request(self, req, cb = None):
  69.         if cb is None:
  70.             cb = req.callback
  71.         
  72.         conn = self.get_connection(req)
  73.         conn.request(req, callback = cb)
  74.  
  75.     
  76.     def get_connection(self, forwhat):
  77.         key = self.key(forwhat)
  78.         
  79.         try:
  80.             conn = self._conns[key]
  81.         except KeyError:
  82.             conn = self._conns[key] = self._make_connection(key)
  83.  
  84.         return conn
  85.  
  86.     
  87.     def _make_connection(self, key):
  88.         log.info('Making new %r for key=%r', self.persister_cls, key)
  89.         (host, s_port) = self.host_port(key)
  90.         port = int(s_port)
  91.         conn = self.persister_cls((host, port))
  92.         if hasattr(self, 'password_mgr'):
  93.             conn.password_mgr = self.password_mgr
  94.         
  95.         self.bind_events(conn)
  96.         return conn
  97.  
  98.     
  99.     def add_password(self, realm, uri, username, password):
  100.         if not hasattr(self, 'password_mgr'):
  101.             self.password_mgr = urllib2.HTTPPasswordMgr()
  102.         
  103.         self.password_mgr.add_password(realm, uri, username, password)
  104.  
  105.     
  106.     def bind_events(self, conn):
  107.         bind = conn.bind_event
  108.         bind('on_fail', self._failed_connection)
  109.         bind('redirect', self._handle_redirect)
  110.         bind('on_close', self._handle_close)
  111.  
  112.     
  113.     def unbind_events(self, conn):
  114.         unbind = conn.unbind
  115.         unbind('on_fail', self._failed_connection)
  116.         unbind('redirect', self._handle_redirect)
  117.         unbind('on_close', self._handle_close)
  118.  
  119.     
  120.     def _handle_redirect(self, req):
  121.         redirect_cb = getattr(req, 'redirect_cb', None)
  122.         newreq = req.on_redirect(req)
  123.         if redirect_cb is not None:
  124.             if newreq is None:
  125.                 req.callback.error('redirect cancelled')
  126.                 return None
  127.             req = newreq
  128.         
  129.         self._do_request(req)
  130.  
  131.     
  132.     def _failed_connection(self, conn):
  133.         log.info('Removing failed connection: conn = %r, key(conn) = %r', conn, self.key(conn))
  134.         self._cleanup(conn)
  135.  
  136.     
  137.     def _handle_close(self, conn):
  138.         self._cleanup(conn)
  139.  
  140.     
  141.     def _cleanup(self, conn):
  142.         self._conns.pop(self.key(conn), None)
  143.         self.unbind_events(conn)
  144.  
  145.     
  146.     def close_all(self):
  147.         while self._conns:
  148.             (_key, conn) = self._conns.popitem()
  149.             self.unbind_events(conn)
  150.             conn.close()
  151.  
  152.  
  153. _httpmaster = HttpMaster()
  154. atexit.register(_httpmaster.close_all)
  155.  
  156. def httpopen(*a, **k):
  157.     cb = k.pop('callback')
  158.     _httpmaster.request(callback = cb, *a, **k)
  159.  
  160. httpopen = util.callsback(httpopen)
  161.  
  162. def main():
  163.     
  164.     def success(*a):
  165.         print 'success', a
  166.  
  167.     
  168.     def error(*a):
  169.         print 'error', a
  170.  
  171.     httpopen('http://65.54.239.211/index.html', success = success, error = error)
  172.  
  173. if __name__ == '__main__':
  174.     from tests.testapp import testapp
  175.     a = testapp()
  176.     main()
  177.     a.MainLoop()
  178.  
  179.