Craig Evans
Craig Evans

Reputation: 173

using geopy and using Nominatim I get certificate verify failed: certificate has expired (_ssl.c:997)

running windows 10 with Python 3.10. I have been running into an SSL error when I attempt to use the Nominatim service. I do not see this error with ArcGIS service.

Example code:

import certifi
import ssl

import geopy.geocoders
from geopy.geocoders import Nominatim
from geopy.geocoders import ArcGIS

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

print('')
print('Trying Nominatim')
try:
    geolocator_NOM = Nominatim(user_agent="NewApp314", timeout=10)
    location_NOM = geolocator_NOM.geocode('1600 Amphitheatre Parkway, Mountain View, CA')

    print(f'Location Address: {location_NOM.address}')
    print(f'Location Latitude: {location_NOM.latitude}')
    print(f'Location Longitude: {location_NOM.longitude}')
except Exception as error:
    print(f'Error in Nominatim: {error}')


print('')
print('Trying ArcGIS:')
try:
    geolocator_ARC = ArcGIS()
    location_ARC = geolocator_ARC.geocode('1600 Amphitheatre Parkway, Mountain View, CA')

    print(f'Location Address: {location_ARC.address}')
    print(f'Location Latitude: {location_ARC.latitude}')
    print(f'Location Longitude: {location_ARC.longitude}')
except Exception as error:
    print(f'Error in ArcGIS: {error}')

When I run the code I receive the following output:

Trying Nominatim:
Error in Nominatim: HTTPSConnectionPool(host='nominatim.openstreetmap.org', port=443): Max retries exceeded with url: /search?q=1600+Amphitheatre+Parkway%2C+Mountain+View%2C+CA&format=json&limit=1 (Caused by 
SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)')))

Trying ArcGIS:
Location Address: 1600 Amphitheatre Pkwy, Mountain View, California, 94043
Location Latitude: 37.421976010046
Location Longitude: -122.084524027111

I've looked all over for solutions and noticed a couple similar errors on stack Overflow, but no solutions:

Geopy Nominatim SSLCertVerificationError

Similar error, but solution did not fix issue identified Python ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)

Upvotes: 3

Views: 337

Answers (1)

Dismiz
Dismiz

Reputation: 1

I had the same issues and started to look into source code, verification of SSL cannot be directly avoided in a way it can be done with requests requests.get(verify=False), but you can do some tweaking around this

  1. Create custom adapter:
from geopy.adapters import RequestsAdapter

class CustomRequestsAdapter(RequestsAdapter):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.session = requests.Session()
        self.session.verify = False
        self.session.trust_env = True
  1. Use this adapter instead of default one:
Nominatim(user_agent="your_app", adapter_factory=CustomRequestsAdapter)

Of course this is not the best solution, as disabling verification is not completely safe, but you can try this

Upvotes: 0

Related Questions