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

jjm*_*nez 6 python windows ssl geolocation python-3.x

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

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)
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我在Python 3.6.0继续Windows 2012 Server

更新

代码如下:

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))
Run Code Online (Sandbox Code Playgroud)

Alm*_*itt 9

我遇到了同样的问题,但最终也必须安装 SSL 和 Certifi。我不确定其他人是否会遇到这个问题,但如果是的话,这就是我解决它的方法。

\n\n

首先安装Certifi和SSL软件包,然后

\n\n
import certifi\nimport ssl\nimport geopy.geocoders\nfrom geopy.geocoders import Nominatim\nctx = ssl.create_default_context(cafile=certifi.where())\ngeopy.geocoders.options.default_ssl_context = ctx\n\ngeolocator = Nominatim(scheme=\'http\')\nlocation = geolocator.reverse("48.8588443, 2.2943506")\n\nprint(location.address)\nprint (location.raw)\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,结果是:

\n\n
Tour Eiffel, 5, Avenue Anatole France, Gros-Caillou, 7e, Paris, \xc3\x8ele-de-France, France m\xc3\xa9tropolitaine, 75007, France\n{\'place_id\': \'62005962\', \'licence\': \'Data \xc2\xa9 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, \xc3\x8ele-de-France, France m\xc3\xa9tropolitaine, 75007, France\', \'address\': {\'attraction\': \'Tour Eiffel\', \'house_number\': \'5\', \'pedestrian\': \'Avenue Anatole France\', \'suburb\': \'Gros-Caillou\', \'city_district\': \'7e\', \'city\': \'Paris\', \'county\': \'Paris\', \'state\': \'\xc3\x8ele-de-France\', \'country\': \'France\', \'postcode\': \'75007\', \'country_code\': \'fr\'}, \'boundingbox\': [\'48.8574753\', \'48.8590465\', \'2.2933084\', \'2.2956897\']}\n
Run Code Online (Sandbox Code Playgroud)\n


jjm*_*nez 3

最后我给出了解决方案:

可能win2012有过时的SSL库。schema因此,解决方案是明确指出http

正确的代码是:

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))
Run Code Online (Sandbox Code Playgroud)

  • 这个答案在 2021 年不起作用,而 Brad Ahrens 的 Windows 10 答案对我有用。 (2认同)