首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python ssl.SSLError:[SSL: CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:748)

Python ssl.SSLError:[SSL: CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:748)
EN

Stack Overflow用户
提问于 2017-10-04 16:18:30
回答 7查看 5.4K关注 0票数 7

我在一个Python 3.6应用程序中使用[geopy][1],我必须在使用Windows 2012 Server的过时机器上运行它。在此服务器上从应用程序调用此库时会出现问题,因为它返回以下错误:

代码语言:javascript
复制
File "C:\ServAPI\Util.py", line 12, in getLocation
location = geolocator.geocode(name)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\osm.py", line 193, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\geopy\geocoders\base.py", line 171, in _call_geocoder
raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)

我如何解决这个问题?我在Windows 2012 Server上运行Python 3.6.0

更新

代码如下:

代码语言:javascript
复制
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut
def getLocation(name):
    geolocator = Nominatim()
    try:
        location = geolocator.geocode(name, timeout=5)
        return location
    except GeocoderTimedOut as e:
        print("Error: geocode failed on input %s with message %s" % (e.msg))
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2017-11-03 16:54:10

最后,我有了解决方案:

可能是win2012SSL库已过期。因此,解决方案是显式地指出schemahttp

正确的代码是:

代码语言:javascript
复制
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderTimedOut

def getLocation(name):
    geolocator = Nominatim(scheme='http')
    try:
        location = geolocator.geocode(name, timeout=5)
        return location
    except GeocoderTimedOut as e:
        print("Error: geocode failed on input %s with message %s" % (e.msg))
票数 2
EN

Stack Overflow用户

发布于 2018-06-03 18:41:59

我也有同样的问题,但最终不得不安装SSL和Certifi。我不确定其他人是否会有这个问题,但如果是的话,下面是我是如何解决这个问题的。

首先安装Certifi和SSL包,然后

代码语言:javascript
复制
import certifi
import ssl
import geopy.geocoders
from geopy.geocoders import Nominatim
ctx = ssl.create_default_context(cafile=certifi.where())
geopy.geocoders.options.default_ssl_context = ctx

geolocator = Nominatim(scheme='http')
location = geolocator.reverse("48.8588443, 2.2943506")

print(location.address)
print (location.raw)

然后,结果是:

代码语言:javascript
复制
Tour Eiffel, 5, Avenue Anatole France, Gros-Caillou, 7e, Paris, Île-de-France, France métropolitaine, 75007, France
{'place_id': '62005962', 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'way', 'osm_id': '5013364', 'lat': '48.8582602', 'lon': '2.29449905431968', 'display_name': 'Tour Eiffel, 5, Avenue Anatole France, Gros-Caillou, 7e, Paris, Île-de-France, France métropolitaine, 75007, France', 'address': {'attraction': 'Tour Eiffel', 'house_number': '5', 'pedestrian': 'Avenue Anatole France', 'suburb': 'Gros-Caillou', 'city_district': '7e', 'city': 'Paris', 'county': 'Paris', 'state': 'Île-de-France', 'country': 'France', 'postcode': '75007', 'country_code': 'fr'}, 'boundingbox': ['48.8574753', '48.8590465', '2.2933084', '2.2956897']}
票数 6
EN

Stack Overflow用户

发布于 2021-10-08 07:46:25

对于最近遇到这个问题的人,这是我在2021年的测试结果:在我的环境中,python 3.6.8 + geopy 2.2.0 + centos 7可以工作;python 2.7.18 + geopy 1.23.0 + centos 8也可以;但python 2.7.5 + geopy 1.23.0 + centos 7不能工作并报告证书错误。

最后,我找到了根本原因,它是由于9月9日到期的原因。LetsEncrypt使用的旧'DST‘根证书的30;请参阅https://letsencrypt.org/docs/dst-root-ca-x3-expiration-september-2021。如果Python使用低于1.1.0的OpenSSL,则会受到影响;请参阅https://openssl.org/blog/blog/2021/09/13/LetsEncryptRootCertExpire

我使用了python 2.7.5 + geopy 1.23.0 + centos 7+ openssl 1.0.2k-fips,解决方法1是从系统证书中删除(DST Root CA X3)根证书,详细步骤如下:

  1. 检查DST根目录在/etc/pki/tls/certs/ca-bundle.crt中,ISRG根目录X1也在中。
  2. 将"# DST根目录CA X3“部分复制到/etc/pki/ca-trust/source/blacklist目录中作为pem文件
  3. 运行DST根目录不再在/etc/pki/tls/certs/ca-bundle.crt中,ISRG根目录X1在ISRG根目录X1中。<代码>H214<代码>G215

参考:

更新:正如@rsc所指出的,更新您的ca证书也可能起作用:

yum upgrade ca-certificates

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46560143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档