[Yum-devel] [PATCH] libproxy support

Zdeněk Pavlas zpavlas at redhat.com
Tue Mar 13 12:14:48 UTC 2012


Based on the work of David Woodhouse <dwmw2 at infradead.org> and
Yan Li <yan.i.li at intel.com>
---
 urlgrabber/grabber.py |   32 ++++++++++++++++++++++++++------
 1 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/urlgrabber/grabber.py b/urlgrabber/grabber.py
index 61e1810..88a01d1 100644
--- a/urlgrabber/grabber.py
+++ b/urlgrabber/grabber.py
@@ -143,8 +143,12 @@ GENERAL ARGUMENTS (kwargs)
     note that proxy authentication information may be provided using
     normal URL constructs:
       proxies={ 'http' : 'http://user:host@foo:3128' }
-    Lastly, if proxies is None, the default environment settings will
-    be used.
+
+  libproxy = True
+
+    Use the libproxy module (if installed) to find proxies.
+    The libproxy code is only used if the proxies dictionary
+    does not provide any proxies.
 
   prefix = None
 
@@ -450,6 +454,12 @@ try:
 except ImportError:
     xattr = None
 
+try:
+    import libproxy
+    _grabber_proxy_factory = libproxy.ProxyFactory()
+except:
+    _grabber_proxy_factory = None
+
 ########################################################################
 #                     MODULE INITIALIZATION
 ########################################################################
@@ -845,6 +855,7 @@ class URLGrabberOptions:
         self.ip_resolve = None
         self.keepalive = 1
         self.proxies = None
+        self.libproxy = True
         self.reget = None
         self.failure_callback = None
         self.interrupt_callback = None
@@ -1275,17 +1286,26 @@ class PyCurlFileObject(object):
             self.curl_obj.setopt(pycurl.MAX_RECV_SPEED_LARGE, int(opts.raw_throttle()))
             
         # proxy settings
+        proxy = None
         if opts.proxies and self.scheme in ('ftp', 'http', 'https'):
+            # use proxies dict
             proxy = opts.proxies.get(self.scheme)
             if proxy is None:
                 if self.scheme == 'http':
                     proxy = opts.proxies.get('https')
                 elif self.scheme == 'https':
                     proxy = opts.proxies.get('http')
-            if proxy and proxy != '_none_':
-                self.curl_obj.setopt(pycurl.PROXY, proxy)
-                self.curl_obj.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_ANY)
-            
+        elif opts.libproxy and _grabber_proxy_factory:
+            # use libproxy
+            for a_proxy in _grabber_proxy_factory.getProxies(self.url):
+                if a_proxy.startswith('http://'):
+                    proxy = a_proxy
+                    if DEBUG: DEBUG.info('using proxy "%s" for url %s' % (proxy, self.url))
+                    break
+        if proxy and proxy != '_none_':
+            self.curl_obj.setopt(pycurl.PROXY, proxy)
+            self.curl_obj.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_ANY)
+
         if opts.username and opts.password:
             if self.scheme in ('http', 'https'):
                 self.curl_obj.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_ANY)
-- 
1.7.4.4



More information about the Yum-devel mailing list