symerio / pgeocode

Postal code geocoding and distance calculation
https://pgeocode.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
238 stars 58 forks source link

HTTP Error 401, Authentication Required #11

Closed paulbradleysmith closed 4 years ago

paulbradleysmith commented 5 years ago

I have pip installed pgeocode with the --user flag as I am on a shared server and lack permissions to write in most places except my home. As a test, I use this demo code:

import pgeocode
nomi = pgeocode.Nominatim('fr')
nomi.query_postal_code("75013")

Is this error below telling me it can't download the geonames database I've asked for?

Thanks.

HTTPError                                 Traceback (most recent call last)
<ipython-input-17-14e76cf7e5d5> in <module>
      1 import pgeocode
----> 2 nomi = pgeocode.Nominatim('fr')
      3 nomi.query_postal_code("75013")

~/.local/lib/python3.7/site-packages/pgeocode.py in __init__(self, country)
     53                           "in 1999.")
     54         self.country = country
---> 55         self._data_path, self._data = self._get_data(country)
     56         self._data_unique = self._index_postal_codes()
     57 

~/.local/lib/python3.7/site-packages/pgeocode.py in _get_data(country)
     69             url = DOWNLOAD_URL.format(country=country)
     70             compression = _infer_compression(url, "zip")
---> 71             reader, encoding, compression = get_filepath_or_buffer(url)[:3]
     72             with ZipFile(reader) as fh_zip:
     73                 with fh_zip.open(country.upper() + '.txt') as fh:

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/site-packages/pandas/io/common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression, mode)
    200 
    201     if _is_url(filepath_or_buffer):
--> 202         req = _urlopen(filepath_or_buffer)
    203         content_encoding = req.headers.get('Content-Encoding', None)
    204         if content_encoding == 'gzip':

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in http_response(self, request, response)
    639         if not (200 <= code < 300):
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 
    643         return response

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in error(self, proto, *args)
    561             http_err = 0
    562         args = (dict, proto, meth_name) + args
--> 563         result = self._call_chain(*args)
    564         if result:
    565             return result

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in http_error_302(self, req, fp, code, msg, headers)
    753         fp.close()
    754 
--> 755         return self.parent.open(new, timeout=req.timeout)
    756 
    757     http_error_301 = http_error_303 = http_error_307 = http_error_302

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 
    533         return response

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in http_response(self, request, response)
    639         if not (200 <= code < 300):
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 
    643         return response

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 
    571 # XXX probably also want an abstract factory that knows when it makes

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result

/opt/anacondaShell/envs/ScientificPython3Stack/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 401: authenticationrequired
rth commented 5 years ago

Is this error below telling me it can't download the geonames database I've asked for?

Yes, as the databases are still in public access http://download.geonames.org/export/zip/ that probably means that you have no direct internet connection on your server.

Likely you would need to configure the proxy used in your network by setting the appropriate environment variables so that requests package (used in pgeocode) is able to use it.

paulbradleysmith commented 5 years ago

Thank you.

rth commented 4 years ago

Closing as resolved.

GlibMartynenko commented 1 year ago

Hi, I was trying to follow your solution @rth but no luck... I see in a code you have this: with urllib.request.urlopen(url) as res: with BytesIO(res.read()) as reader: if url.endswith(".zip"): with ZipFile(reader) as fh_zip: with fh_zip.open(country.upper() + ".txt") as fh: yield fh else: yield reader

so, there is no way to pass proxy settings inside of urllib request... Could you please check if some solution exists?