PYTHON 12
Libproxy.py Guest on 3rd October 2020 02:58:13 PM
  1. ###############################################################################
  2. # libproxy - A library for proxy configuration
  3. # Copyright (C) 2006 Nathaniel McCallum <[email protected]>
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2.1 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public
  16. # License along with this library; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  18. ###############################################################################
  19.  
  20. "A library for proxy configuration and autodetection."
  21.  
  22. import ctypes
  23. import ctypes.util
  24. import platform
  25.  
  26. import sys
  27.  
  28. # Load C library
  29. if platform.system() == "Windows":
  30.     _libc = ctypes.cdll.msvcrt
  31. else:
  32.     if not ctypes.util.find_library("c"):
  33.         raise ImportError("Unable to import C Library!?!")
  34.     _libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c"))
  35.  
  36.  
  37. # Load libproxy
  38. if not ctypes.util.find_library("proxy"):
  39.     raise ImportError("Unable to import libproxy!?!?")
  40.  
  41.  
  42. _libproxy = ctypes.cdll.LoadLibrary(ctypes.util.find_library("proxy"))
  43. _libproxy.px_proxy_factory_get_proxies.restype = ctypes.POINTER(ctypes.c_void_p)
  44.  
  45. class ProxyFactory(object):
  46.     """A ProxyFactory object is used to provide potential proxies to use
  47.    in order to reach a given URL (via 'getProxies(url)').
  48.  
  49.    This instance should be kept around as long as possible as it contains
  50.    cached data to increase performance.  Memory usage should be minimal (cache
  51.    is small) and the cache lifespan is handled automatically.
  52.  
  53.    Usage is pretty simple:
  54.        pf = libproxy.ProxyFactory()
  55.        for url in urls:
  56.            proxies = pf.getProxies(url)
  57.            for proxy in proxies:
  58.                if proxy == "direct://":
  59.                    # Fetch URL without using a proxy
  60.                elif proxy.startswith("http://"):
  61.                    # Fetch URL using an HTTP proxy
  62.                elif proxy.startswith("socks://"):
  63.                    # Fetch URL using a SOCKS proxy
  64.                
  65.                if fetchSucceeded:
  66.                    break    
  67.    """
  68.  
  69.     class ProxyResolutionError(RuntimeError):

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.