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