[dc5ef15] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | import urllib2 |
---|
| 4 | import sys |
---|
| 5 | import json |
---|
| 6 | import logging |
---|
| 7 | import re |
---|
| 8 | |
---|
| 9 | ''' |
---|
| 10 | HTTP Proxy parser and Connection |
---|
| 11 | |
---|
| 12 | connect() function: |
---|
| 13 | - auto detects proxy in windows, osx |
---|
| 14 | - in ux systems, the http_proxy enviroment variable must be set |
---|
| 15 | - if it fails, try to find the proxy.pac address. |
---|
| 16 | - parses the file, and looks up for all possible proxies |
---|
| 17 | ''' |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | class ConnectionProxy(object): |
---|
| 21 | |
---|
| 22 | def __init__(self, url, timeout): |
---|
| 23 | self.url = url |
---|
| 24 | self.timeout = timeout |
---|
| 25 | |
---|
| 26 | def _get_addresses_of_proxy_pac(self): |
---|
| 27 | """ |
---|
| 28 | Return a list of possible auto proxy .pac files being used, |
---|
| 29 | based on the system registry (win32) or system preferences (OSX). |
---|
| 30 | @return: list of urls |
---|
| 31 | """ |
---|
| 32 | pac_files = [] |
---|
| 33 | if sys.platform == 'win32': |
---|
| 34 | try: |
---|
| 35 | import _winreg as winreg # used from python 2.0-2.6 |
---|
| 36 | except: |
---|
| 37 | import winreg # used from python 2.7 onwards |
---|
| 38 | net = winreg.OpenKey( |
---|
| 39 | winreg.HKEY_CURRENT_USER, |
---|
| 40 | "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" |
---|
| 41 | ) |
---|
| 42 | n_subs, n_vals, last_mod = winreg.QueryInfoKey(net) |
---|
| 43 | subkeys = {} |
---|
| 44 | for i in range(n_vals): |
---|
| 45 | this_name, this_val, this_type = winreg.EnumValue(net, i) |
---|
| 46 | subkeys[this_name] = this_val |
---|
| 47 | if 'AutoConfigURL' in subkeys.keys() and len(subkeys['AutoConfigURL']) > 0: |
---|
| 48 | pac_files.append(subkeys['AutoConfigURL']) |
---|
| 49 | elif sys.platform == 'darwin': |
---|
| 50 | import plistlib |
---|
| 51 | sys_prefs = plistlib.readPlist( |
---|
| 52 | '/Library/Preferences/SystemConfiguration/preferences.plist') |
---|
| 53 | networks = sys_prefs['NetworkServices'] |
---|
| 54 | # loop through each possible network (e.g. Ethernet, Airport...) |
---|
| 55 | for network in networks.items(): |
---|
| 56 | # the first part is a long identifier |
---|
| 57 | net_key, network = network |
---|
| 58 | if 'ProxyAutoConfigURLString' in network['Proxies'].keys(): |
---|
| 59 | pac_files.append( |
---|
| 60 | network['Proxies']['ProxyAutoConfigURLString']) |
---|
| 61 | return list(set(pac_files)) # remove redundant ones |
---|
| 62 | |
---|
| 63 | def _parse_proxy_pac(self, pac_urls_list): |
---|
| 64 | ''' |
---|
| 65 | For every pac file url in pac_urls_list, it tryes to connect. |
---|
| 66 | If the connection is successful parses the file in search for |
---|
| 67 | http proxies. |
---|
| 68 | @param pac_urls_list: List with urls for the pac files |
---|
| 69 | @return: list with all found http proxies |
---|
| 70 | ''' |
---|
| 71 | proxy_url_list = [] |
---|
| 72 | for this_pac_url in pac_urls_list: |
---|
| 73 | logging.debug('Trying pac file (%s)...' % this_pac_url) |
---|
| 74 | try: |
---|
| 75 | response = urllib2.urlopen( |
---|
| 76 | this_pac_url, timeout=self.timeout) |
---|
| 77 | logging.debug('Succeeded (%s)...' % this_pac_url) |
---|
| 78 | except Exception: |
---|
| 79 | logging.debug('Failled (%s)...' % this_pac_url) |
---|
| 80 | continue |
---|
| 81 | pacStr = response.read() |
---|
| 82 | possProxies = re.findall( |
---|
| 83 | r"PROXY\s([^\s;,:]+:[0-9]{1,5})[^0-9]", pacStr + '\n') |
---|
| 84 | for thisPoss in possProxies: |
---|
| 85 | prox_url = 'http://' + thisPoss |
---|
| 86 | proxy_dic = {'http': prox_url} |
---|
| 87 | proxy_url_list.append(proxy_dic) |
---|
| 88 | return proxy_url_list |
---|
| 89 | |
---|
| 90 | def _set_proxy(self,proxy_dic=None): |
---|
| 91 | ''' |
---|
| 92 | Sets connection proxy. |
---|
| 93 | if proxy_dic is None get's teh proxy from the system. |
---|
| 94 | To disable autodetected proxy pass an empty dictionary: {} |
---|
| 95 | @param proxy_dic: format: {'http': 'http://www.example.com:3128/'} |
---|
| 96 | ''' |
---|
| 97 | if proxy_dic is None: |
---|
| 98 | # The default is to read the list of proxies from the environment variables <protocol>_proxy. |
---|
| 99 | # If no proxy environment variables are set, then in a Windows environment proxy settings are |
---|
| 100 | # obtained from the registry's Internet Settings section, and in a Mac OS X environment proxy |
---|
| 101 | # information is retrieved from the OS X System Configuration |
---|
| 102 | # Framework. |
---|
| 103 | proxy = urllib2.ProxyHandler() |
---|
| 104 | else: |
---|
| 105 | # If proxies is given, it must be a dictionary mapping protocol names to |
---|
| 106 | # URLs of proxies. |
---|
| 107 | proxy = urllib2.ProxyHandler(proxy_dic) |
---|
| 108 | opener = urllib2.build_opener(proxy) |
---|
| 109 | urllib2.install_opener(opener) |
---|
| 110 | |
---|
| 111 | def connect(self): |
---|
| 112 | ''' |
---|
| 113 | Performs the request and gets a response from self.url |
---|
| 114 | @return: response object from urllib2.urlopen |
---|
| 115 | ''' |
---|
| 116 | req = urllib2.Request(self.url) |
---|
| 117 | response = None |
---|
| 118 | try: |
---|
| 119 | logging.debug("Trying Direct connection to %s..."%self.url) |
---|
| 120 | response = urllib2.urlopen(req, timeout=self.timeout) |
---|
| 121 | except Exception, e: |
---|
| 122 | logging.debug("Failed!") |
---|
| 123 | logging.debug(e) |
---|
| 124 | try: |
---|
| 125 | logging.debug("Trying to use system proxy if it exists...") |
---|
| 126 | self._set_proxy() |
---|
| 127 | response = urllib2.urlopen(req, timeout=self.timeout) |
---|
| 128 | except Exception, e: |
---|
| 129 | logging.debug("Failed!") |
---|
| 130 | logging.debug(e) |
---|
| 131 | pac_urls = self._get_addresses_of_proxy_pac() |
---|
| 132 | proxy_urls = self._parse_proxy_pac(pac_urls) |
---|
| 133 | for proxy in proxy_urls: |
---|
| 134 | try: |
---|
| 135 | logging.debug("Trying to use the proxy %s found in proxy.pac configuration"%proxy) |
---|
| 136 | self._set_proxy(proxy) |
---|
| 137 | response = urllib2.urlopen(req, timeout=self.timeout) |
---|
| 138 | except Exception, e: |
---|
| 139 | logging.debug("Failed!") |
---|
| 140 | logging.debug(e) |
---|
| 141 | if response is not None: |
---|
| 142 | logging.debug("The connection to %s was successful."%self.url) |
---|
| 143 | else: |
---|
| 144 | logging.warning("Connection to %s failed..."%self.url) |
---|
| 145 | return response |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | if __name__ == "__main__": |
---|
| 149 | from pprint import pprint |
---|
| 150 | c = Connection() |
---|
| 151 | response = c.connect() |
---|
| 152 | if response is not None: |
---|
| 153 | print 50 * '-' |
---|
| 154 | content = json.loads(response.read().strip()) |
---|
| 155 | pprint(content) |
---|
| 156 | |
---|