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

[Solved] Import from first hard coded URL fails with "[SSL: CERTIFICATE_VERIFY_FAILED" #54

Open mborus opened 3 years ago

mborus commented 3 years ago

On windows 10 using Python 3.9.7 the quickstart example crashes at

"nomi = pgeocode.Nominatim('fr')"

with the error

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:     certificate has expired (_ssl.c:1129)>

This is probably caused by the Let's Encrypt certificate.

You can do a quick fix by either removing the first URL in line 25 in pgeocode.py or by turning off ssl verification in line 141

import ssl
with urllib.request.urlopen(url, context=ssl._create_unverified_context()) as 
mborus commented 3 years ago

Update: This seems to be a general Windows Python problem and it's fixable by manually installing the "ISRG Root X1" (https://letsencrypt.org/docs/certificate-compatibility/) - http://woshub.com/updating-trusted-root-certificates-in-windows-10/

Still it would be nice if an SSL error with the first URL would not stop the second one from working.

paul-rupf commented 2 years ago

Hi, I still have the same error when initiating pgeocode.Nominatim. I tried to fix the code of pgeocode as suggusted and checked, if ISRG ROOT X1 is installed on my system (Win11). Still, I get the URLError mentioned before.

Do you have any ideas how to fix this issue?

Thanks, Paul

airqualityanthony commented 2 years ago

You can add ssl._create_default_https_context = ssl._create_unverified_context in line 140 and 158 in pgeocode.py as a temp.

soysushi commented 2 years ago

Is there another way to solve this issue? adding ssl._create_default is apparently called monkey-patching which is generally not a good practice.

having this same issue here.

gbilleyPeco commented 1 year ago

Has there been any fix for this issue? I'm running into the same problem. The monkey-patching suggested by @mborus did not work.

Thanks!

AntmanLFEz commented 1 year ago

Has there been any fix for this issue? I'm running into the same problem. The monkey-patching suggested by @mborus did not work.

Thanks!

I was still unable to get letsencrypt certificates validated even when ensuring "ISRG Root X1" and "ISRG Root X2" were installed.

The simplest solution was to go into pgeocode.py and remove the url starting with https://download.geonames.org from the DOWNLOAD_URL array. The remaining url uses a digicert which has not issues with urllib.

gbilleyPeco commented 1 year ago

Has there been any fix for this issue? I'm running into the same problem. The monkey-patching suggested by @mborus did not work. Thanks!

I was still unable to get letsencrypt certificates validated even when ensuring "ISRG Root X1" and "ISRG Root X2" were installed.

The simplest solution was to go into pgeocode.py and remove the url starting with https://download.geonames.org from the DOWNLOAD_URL array. The remaining url uses a digicert which has not issues with urllib.

Wow, that worked! I can't believe it. Thank you!

nagoli commented 2 months ago

you can also define the DOWNLOAD_URL var while running pgeocode and remove the s of https url : pgeocode.DOWNLOAD_URL = [ "http://download.geonames.org/export/zip/{country}.zip", "http://symerio.github.io/postal-codes-data/data/geonames/{country}.txt", ]