def __init__( self, format_string=DEFAULT_FORMAT_STRING, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None ): """ Mostly-common geocoder validation, proxies, &c. Not all geocoders specify format_string and such. """ self.format_string = format_string self.scheme = scheme if self.scheme not in ('http', 'https'): # pragma: no cover raise ConfigurationError( 'Supported schemes are `http` and `https`.' ) self.proxies = proxies self.timeout = timeout if self.proxies: install_opener( build_opener( ProxyHandler(self.proxies) ) ) self.urlopen = urllib_urlopen
def __init__( self, format_string=DEFAULT_FORMAT_STRING, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None, user_agent=None, ): """ Mostly-common geocoder validation, proxies, &c. Not all geocoders specify format_string and such. """ self.format_string = format_string self.scheme = scheme if self.scheme not in ('http', 'https'): # pragma: no cover raise ConfigurationError( 'Supported schemes are `http` and `https`.' ) self.proxies = proxies self.timeout = timeout self.headers = {'User-Agent': user_agent or DEFAULT_USER_AGENT} if self.proxies: opener = build_opener( ProxyHandler(self.proxies) ) self.urlopen = opener.open else: self.urlopen = urllib_urlopen
def addSimpleHTTPAuthHeader(self): """ Create Urllib request object embedding HTTP simple authentication """ sub_request = """ <GeocodeRequest returnFreeForm="{is_freeform}"> <Address countryCode="{query_type}"> <freeFormAddress>{query}</freeFormAddress> </Address> </GeocodeRequest> """ xml_request = self.xml_request.format( method_name='LocationUtilityService', sub_request=sub_request, maximum_responses=1 ) # Create query using parameters request_string = xml_request.format( is_freeform='false', query='rennes', query_type='PositionOfInterest' ) params = { 'xls': request_string } top_level_url = "?".join((self.api, urlencode(params))) password_mgr = HTTPPasswordMgrWithDefaultRealm() # Add the username and password. # If we knew the realm, we could use it instead of None. password_mgr.add_password( None, top_level_url, self.username, self.password ) handler = HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) opener = build_opener(handler) # Install the opener. # Now all calls to urllib.request.urlopen use our opener. install_opener(opener)
def __init__(self, format_string=DEFAULT_FORMAT_STRING, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None): """ Mostly-common geocoder validation, proxies, &c. Not all geocoders specify format_string and such. """ self.format_string = format_string self.scheme = scheme if self.scheme not in ('http', 'https'): # pragma: no cover raise ConfigurationError( 'Supported schemes are `http` and `https`.') self.proxies = proxies self.timeout = timeout if self.proxies: install_opener(build_opener(ProxyHandler(self.proxies))) self.urlopen = urllib_urlopen
def __init__(self, format_string=DEFAULT_FORMAT_STRING, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None): """ Mostly-common geocoder validation, proxies, &c. Not all geocoders specify format_string and such. """ self.format_string = format_string self.scheme = scheme if self.scheme not in ('http', 'https'): # pragma: no cover raise ConfigurationError('Supported schemes are `http` and `https`.') self.proxies = proxies self.timeout = timeout # Add urllib proxy support using environment variables or # built in OS proxy details # See: http://docs.python.org/2/library/urllib2.html # And: http://stackoverflow.com/questions/1450132/proxy-with-urllib2 if self.proxies is None: self.urlopen = urllib_urlopen else: self.urlopen = build_opener( ProxyHandler(self.proxies) )