Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in geocoding example in README? #124

Closed
khorton opened this issue May 6, 2015 · 10 comments
Closed

Error in geocoding example in README? #124

khorton opened this issue May 6, 2015 · 10 comments

Comments

@khorton
Copy link

khorton commented May 6, 2015

The first example in the README for v1.1.0 appears to have an error. If I run the example verbatim from the README, I get:

geolocator = Nominatim()
location = geolocator.geocode("175 5th Avenue NYC")
Traceback (most recent call last):
File "", line 1, in
File "/sw/lib/python2.7/site-packages/geopy/geocoders/osm.py", line 192, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "/sw/lib/python2.7/site-packages/geopy/geocoders/base.py", line 160, in _call_geocoder
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>

The example works without error if I add "scheme='http'" to the call to Nominatim(). E.g.

geolocator = Nominatim(scheme='http')
location = geolocator.geocode("175 5th Avenue NYC")
print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, 10010, United States of America

@ijl
Copy link
Member

ijl commented May 7, 2015

@khorton I can't reproduce. That your exception is a GeocoderServiceError means a request was sent. Possibly this was a transitory network or Nominatim (502) issue? Is it consistent for you?

@khorton
Copy link
Author

khorton commented May 7, 2015

I get the same errors today, on two different computers.

I note that the comments in geopy.geocoders.Nominatim state that "Nominatim does not support SSL". Does it support https, if it does not support SSL? When I saw the SSL-related error message, and the note in the docs that SSL is not supported, I looked for a way to ensure SSL was not involved, which lead me to "scheme='http'". What default scheme do you have set in your geopy?

Kevin Horton

@criptych
Copy link

criptych commented Jun 4, 2015

Does it support https, if it does not support SSL?

"https" basically means HTTP over SSL, so no.

I looked for a way to ensure SSL was not involved, which lead me to "scheme='http'". What default scheme do you have set in your geopy?

It does look like the default scheme is still "https" since it's imported from geocoders.base.

@AbdealiLoKo
Copy link

I got a very similar issue. For me .geocode() example worked find. But the .reverse() example didn't. It gave the Service Not available exception.

On using scheme='http' it worked fine.

@tmarthal
Copy link

tmarthal commented Apr 17, 2017

With the python 3.6 changes to how certifications are handled, I thought I'd share my solution to having ssl problems with the geocoder example by monkey patching the urlopen attribute using the certifi certs:

Problematic code with no certifications:

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")

<removed stack trace>
    page = requester(req, timeout=(timeout or self.timeout), **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>

Here we pass the certifi certificates into the urlopen method:

>>> from geopy.geocoders import Nominatim
>>> import certifi
>>> def uo(args, **kwargs):
...    return urllib.request.urlopen(args, cafile=certifi.where(), **kwargs)
... 
>>> geolocator = Nominatim()
>>> geolocator.urlopen = uo 
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> location.address
'Flatiron Building, 175, 5th Avenue, Flatiron, Manhattan, New York County, NYC, New York, 10010, United States of America'

Not sure if it helps this old issue, but that was the solution I came up with.

@abhi007tyagi
Copy link

abhi007tyagi commented Aug 4, 2017

where to set scheme=http?

I am also seeing SSL error on following example

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York, ...
>>> print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
>>> print(location.raw)
{'place_id': '9167009604', 'type': 'attraction', ...}

@dterg
Copy link

dterg commented Mar 6, 2018

Pass it as an argument to the Nominatim class:

geolocator = Nominatim(scheme='http)

@KostyaEsmukov
Copy link
Member

#291 has added support for passing custom ssl_context.

After geopy 1.14.0 is released (that'll be pretty soon), for anyone affected by this I suggest to do the following:

  • If you're still on an older version of Python (before 2.7.9 and 3.4.3), please consider upgrading. Otherwise a warning will be issued stating that TLS verification doesn't happen.

  • If you ended up using scheme='http', consider switching back to the default https scheme along with passing a custom SSL context.

  • If you were monkey-patching urlopen (as in the comment by @tmarthal above), please stop that, and use the following instead:

    import certifi
    import ssl
    import geopy.geocoders
    ctx = ssl.create_default_context(cafile=certifi.where())
    geopy.geocoders.options.default_ssl_context = ctx
    

@KostyaEsmukov
Copy link
Member

geopy==1.14.0 has been released 🎉

@gusleig
Copy link

gusleig commented Sep 28, 2018

Im getting AttributeError: module 'geopy.geocoders' has no attribute 'options'

and same SSL error

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants