home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / common / asynchttp / requester.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  5.5 KB  |  159 lines

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